【发布时间】:2014-10-02 08:16:19
【问题描述】:
我已经搜索了整个网络,但似乎无法找到解决这个简单问题的方法。当工作“喜欢”时,我有一个按钮的表格视图。按下按钮时,它会将单词更改为“Unlike”。我让它工作但是当我向下滚动表格时,我看到其他按钮也变为“不喜欢”,有时会与“喜欢”重叠。当我向上滚动时,我选择的原始按钮会变回正常状态。我知道单元格是可重用的,这就是为什么我为我的数据源使用可变数组但它仍然不起作用的原因。请帮忙!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setTitle:@"Like" forState:UIControlStateNormal];
myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(14.0, 10.0, 125.0, 25.0);
myButton.tag =indexPath.row;
[cell.contentView addSubview:myButton];
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
动作方法:
-(void)tapped:(id)sender {
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *parentCell = [[sender superview]superview];
NSIndexPath *indexPathLiked = [table indexPathForCell:parentCell];
[array replaceObjectAtIndex:senderButton.tag withObject:[NSNumber numberWithInt:1]];
[sender setTitle:@"Unlike" forState:UIControlStateNormal];
}
【问题讨论】:
标签: ios uitableview ios7 uibutton