Reputation: 129
So i embedded two buttons within each table cell in my Uitableview. I allow the user to edit the button title. However as soon as the user scrolls the cell off the screen, the title becomes blank again. Any idea how to fix this? Here is my code:
TableView Method:
- (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;
}
Edit Button Title Method
-(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];
}
Upvotes: 2
Views: 226
Reputation: 38667
As commented by bobnoble, you need to also set the titles in tableView:cellForRowAtIndexPath:
, because this is where you're creating new (or reusing different) instances of the buttons. So
insert before return cell
something that will fetch and set the edited title. Something along the lines of:
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];
Upvotes: 1