所以我在Uitableview中的每个表格单元格中嵌入了两个按钮。我允许用户编辑按钮标题。但是,只要用户将单元格从屏幕滚动,标题就会再次变为空白。知道如何解决这个问题吗?这是我的代码:
TableView方法:
- (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];
}
答案 0 :(得分:0)
在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];
[编辑:哦,我的,Stackoverflow告诉我这个问题不到5分钟......一定是时钟问题了]