问题描述
|
编辑:我开始对此悬赏,因为我正在寻找带有
UITableViewCell
子类的“更清洁”解决方案,而不是弄乱UITableView
委托和数据源。理想情况下,我想看看如何处理UISlider
事件。谢谢!
我正在使用组样式的UITableView
,并且有几个像这样的单元格:
现在,当我处于编辑模式时,我想有一个UISlider
来设置正确的UILabel的值,以便结果如下所示:
有人可以指出我正确的方向吗?我什至不确定是否应该通过将UITableViewCell
子类化或在IB中做到这一点。代码片段将不胜感激:)
解决方法
首先,创建一个新的基于导航的项目,然后分别创建一个名为class6ѭ和
CustomCell.m
的新类文件。
将此代码复制并粘贴到您的RootViewController.m文件中:
#import \"RootViewController.h\"
#import \"CustomCell.h\"
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @\"Cell\";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.mainLabel.text=@\"ValueName\";
return cell;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView.editing){
return 70;
}
return 44;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
return;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[super dealloc];
}
@end
CustomCell.h:
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
}
@property (nonatomic,retain) UILabel *mainLabel;
@property (nonatomic,retain) UILabel *detailLabel;
@property (nonatomic,retain) UISlider *slider;
@end
CustomCell.m:
#import \"CustomCell.h\"
@implementation CustomCell
@synthesize mainLabel,detailLabel,slider;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
slider = [[UISlider alloc] initWithFrame:CGRectMake(10,12,280,0)];
slider.alpha = 0;
slider.maximumValue = 30;
slider.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.contentView addSubview:slider];
[slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventTouchDragInside];
[slider release];
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,150,20)];
mainLabel.highlightedTextColor = [UIColor whiteColor];
mainLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:mainLabel];
[mainLabel release];
detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width-180,20)];
detailLabel.textAlignment=UITextAlignmentRight;
detailLabel.text = [NSString stringWithFormat:@\"%i\",lroundf(slider.value)];
detailLabel.highlightedTextColor = [UIColor whiteColor];
detailLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:detailLabel];
[detailLabel release];
}
return self;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:YES];
if(editing){
slider.alpha = 1;
slider.userInteractionEnabled=YES;
}else{
slider.alpha = 0;
slider.userInteractionEnabled=NO;
}
[UIView commitAnimations];
}
-(IBAction) sliderChanged:(id) sender{
detailLabel.text=[NSString stringWithFormat:@\"%i\",lroundf(slider.value)];
}
- (void)dealloc
{
[super dealloc];
}
@end
编译并运行,您便拥有了所需控件的完美版本。我什至实现了像黄油一样流畅的动画。玩得开心!
编辑:
如果确实要使用编辑控件,则必须不要实现以下方法,而必须调整标签和滑块的框架。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
,作为赏金的傻瓜,我坐下来试验了一下。这是我发现的最干净的解决方案:
您将创建一个自定义单元格类。正如我从您的帖子中看到的那样,您对此没有任何问题,我相信这确实是唯一的方法。
在头文件中:
@interface TableCellSlider : UITableViewCell {
UISlider *cellSlider;
}
在主文件中:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
cellSlider = [[UISlider alloc] initWithFrame:CGRectMake(200,10,100,20)];
[self addSubview:cellSlider];
cellSlider.hidden = YES;
}
return self;
}
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
if (self.editing) {
cellSlider.hidden = NO;
} else {
cellSlider.hidden = YES;
}
}
- (float)getValue {
return cellSlider.value;
}
至于表格和单元格的高度,将其添加到表格脚本中:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[self.tableView beginUpdates];
[super setEditing:editing animated:YES];
[self.tableView endUpdates];
//[self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing) {
return 50;
} else {
return 30;
}
}
这应该可以解决问题。您可以在单元格上使用getValue,因此不必合成滑块,并且无论单元格变为可编辑的任何原因,它们都将隐藏/重新出现。
祝您的项目一切顺利:]
,您需要在名为delgate的方法中重新配置单元
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
当用户交换要删除的单元格时,将调用此方法
您可以在其中通过UITableView的方法找到单元格
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
然后重新配置
有关更多信息,请参阅开发人员文档。
,就我所知,您可以在编辑开始时添加滑块。我的意思是以下方法。
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
/* Add your slider. */
}
谢谢。
,我只是盲目射击,因为我还没有自己实现,但是,以新大小重新加载表可能会有所帮助,您可以在处于编辑模式时按照以下方法进行覆盖。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (int)yourHeight;
}