选择单元格会显示一个按钮,取消选择会隐藏按钮

时间:2014-11-26 21:08:43

标签: ios objective-c uitableview nsindexpath

我想要一个按钮来显示我何时选择一个单元格,当我选择相同的单元格或另一个单元格时,我希望按钮隐藏。所以一个按钮应该一次只能看到。

我有什么:

我的.h文件中的

@property (strong, nonatomic) NSIndexPath *isselectednow;

和我的.m文件:

viewDidLoad中:

self.isselectednow = [NSIndexPath indexPathForRow:7 inSection:1];

的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell";

 UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
 }


 //[cell addSubview:[self drawSeparationView:(indexPath.row)]];

 UILabel *DateName = (UILabel *)[cell viewWithTag:100100];
 DateName.textColor = [UIColor blackColor];
 //DateName.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row ];
 DateName.text = objects[indexPath.row];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(buttonOnCellPressed:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Update" forState:UIControlStateNormal];
button.frame = CGRectMake(DEVICE_SIZE.width - 60, 18, 55, 15);

[cell addSubview:button];
if ([indexPath compare:self.isselectednow] == NSOrderedSame) {
    button.hidden = NO;
}
else{
    button.hidden = YES;
}

 UILabel *lbl1 = (UILabel *)[cell viewWithTag:100101];
 lbl1.textColor = UIColorFromRGB(0x141414);
 UILabel *lbl2 = (UILabel *)[cell viewWithTag:100102];
 lbl2.textColor = UIColorFromRGB(0x141414);
 UILabel *lbl3 = (UILabel *)[cell viewWithTag:100103];
 lbl3.textColor = UIColorFromRGB(0x141414);
 UILabel *lbl4 = (UILabel *)[cell viewWithTag:100104];
 lbl4.textColor = UIColorFromRGB(0x141414);
 UILabel *lbl5 = (UILabel *)[cell viewWithTag:100105];
 lbl5.textColor = UIColorFromRGB(0x141414);

 UITextField *breakfastName = (UITextField *) [cell viewWithTag:100200];
 breakfastName.attributedPlaceholder =
 [[NSAttributedString alloc]
  initWithString:@"Name here"
  attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
 breakfastName.textColor = UIColorFromRGB(0x141414);
 [breakfastName setDelegate:self];
 //[breakfastName addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];

 UITextField *OutName = (UITextField *) [cell viewWithTag:100201];
 OutName.attributedPlaceholder =
 [[NSAttributedString alloc]
  initWithString:@"Name here"
  attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
 OutName.textColor = UIColorFromRGB(0x141414);
 [OutName setDelegate:self];

 UITextField *lunchName = (UITextField *) [cell viewWithTag:100202];
 lunchName.attributedPlaceholder =
 [[NSAttributedString alloc]
  initWithString:@"Name here"
  attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
 lunchName.textColor = UIColorFromRGB(0x141414);
 [lunchName setDelegate:self];

 UITextField *InName = (UITextField *) [cell viewWithTag:100203];
 InName.attributedPlaceholder =
 [[NSAttributedString alloc]
  initWithString:@"Name here"
  attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
 InName.textColor = UIColorFromRGB(0x141414);
 [InName setDelegate:self];

 UITextField *eveningName = (UITextField *) [cell viewWithTag:100204];
 eveningName.attributedPlaceholder =
 [[NSAttributedString alloc]
  initWithString:@"Name here"
  attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
 eveningName.textColor = UIColorFromRGB(0x141414);
 [eveningName setDelegate:self];

 UITextField *othersName = (UITextField *) [cell viewWithTag:100205];
 othersName.attributedPlaceholder =
 [[NSAttributedString alloc]
 initWithString:@"Name here"
 attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
 othersName.textColor = UIColorFromRGB(0x141414);



 cell.clipsToBounds = YES;
 cell.accessoryType = UITableViewCellAccessoryNone;
 cell.backgroundColor = UIColorFromRGB(0xf3f0e9);

返回细胞;  }

didSelectRowAtIndexPath方法:

[tableView beginUpdates];
if ([indexPath compare:self.isselectednow] == NSOrderedSame) {
    self.isselectednow = nil;
}
else{
    self.isselectednow = indexPath;
}

[tableView endUpdates];
//[tableView reloadData];

enter image description here

1 个答案:

答案 0 :(得分:1)

来自docs about beginUpdates

  

如果要同时为后续插入,删除和选择操作(例如,cellForRowAtIndexPath:和indexPathsForVisibleRows)设置动画,请调用此方法。

虽然您已更改isSelectedNow布尔值,但您实际上并未在beginUpdatesendUpdates之间指定任何插入,删除或选择操作。因此,您实际上并未开始或结束对单元格的任何更新。内容与该代码。

我不确定您要尝试做什么,但取出[tableView beginUpdates];[tableView beginUpdates];并重新插入[tableView reloadData];会导致正确的数据重新加载。< / p>

编辑:

除此之外,问题在于您使用dequeueReusableCellWithIdentifier:重复使用自己的单元格,但每次都要添加新的UIButton子视图,因此您需要基本上把每个新UIButton放在旧的cellForRowAtIndexPath:上。我建议您为viewWithTag:中的所有标签和文本字段执行的操作 - 重复使用更新按钮,方法是将其添加到自定义表格视图单元格并使用{{1}}访问它。