ios - 滚动表格时按钮文本消失

标签 ios xcode uitableview uibutton

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

表格 View 方法:

- (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];

 }

最佳答案

正如 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];

关于ios - 滚动表格时按钮文本消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496930/

相关文章:

iphone - 如何使用simpleaudioengine播放指定时长的音频

ios - UI 不会立即更新 iOS

ios - 自动调整 UITableView 中嵌入的 UICollectionView 单元格大小不起作用

ios - 无法从 didSelectRowAtIndexPath 引用 cellForRowAtIndexPath 中的 IBoutlet

ios - 获取 Google Maps 步行路线 JSON 而不是驾驶

objective-c - 新手询问从 NSMutableArray 读取 NSMutableArray 并将 NSMutableArray 保存到文档文件

javascript - 有没有办法为移动设备实现带有视频聊天的 PWA?

ios - 如何将从相机拍摄的图像缩小到 320x320 分辨率?

xcode - Swift:通过代码更改 Storyboard上元素的位置

ios - 如何在ios中调整单元格高度和行高