2

所以我在我的 Uitableview 的每个表格单元格中嵌入了两个按钮。我允许用户编辑按钮标题。但是,一旦用户将单元格滚动出屏幕,标题就会再次变为空白。知道如何解决这个问题吗?这是我的代码:

表视图方法:

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

  static NSString *cellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if(cell == nil){
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }

  addWeightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  addRepButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [addWeightButton addTarget:self action:@selector(weightPressedAction:) forControlEvents:UIControlEventTouchUpInside];
  [addRepButton addTarget:self action:@selector(repPressedAction:) forControlEvents:UIControlEventTouchUpInside];
  addWeightButton.frame = CGRectMake(110.0f, 5.0f, 75.0f, 30.0f);
  addRepButton.frame = CGRectMake(260.0f, 5.0f, 50.0f, 30.0f);
  [addWeightButton setTag:indexPath.row];
  [addRepButton setTag:indexPath.row];
  [cell addSubview:addWeightButton];
  [cell addSubview:addRepButton];
  return cell; 
}

编辑按钮标题方法

-(void) editWeightNumber:(int)value {
  [[routine objectAtIndex:index] addWeight:value atIndex:selectedWeightBox];
  NSString* weights = [NSString stringWithFormat:@"%i", [[routine objectAtIndex:index] getWeight:selectedWeightBox]];
  [weightSender setTitle:weights forState:UIControlStateNormal];

 }
4

1 回答 1

1

正如 bobnoble 所评论的,您还需要在 中设置标题tableView:cellForRowAtIndexPath:,因为这是您创建新的(或重用不同的)按钮实例的地方。因此,在return cell将获取并设置已编辑标题的内容之前插入。类似于以下内容:

id routineObject = [routine objectAtIndex:indexPath.row];
NSString *weights = [NSString stringWithFormat:@"%i", [routineObject getWeight:addWeightBox]];
[addWeightButton setTitle:weights forState:UIControlStateNormal];
NSString *reps = [NSString stringWithFormat:@"%i", [routineObject getWeight:addRepBox]];
[addRepButton setTitle:reps forState:UIControlStateNormal];
于 2013-10-14T08:04:04.610 回答