访问UITableView单元格

时间:2015-09-10 21:55:05

标签: ios uitableview uibutton

我在单元格上有一个相似的按钮,旁边显示的是喜欢的数量。 我已经在cellForRowAtIndexPath中创建了所有单元格,并且我用它的indexPath标记了每个单元格。现在,当有人点击"喜欢"按钮,我想显示更新的数字,而无需重新加载整个表。

为了简单起见,我将展示我的代码的准系统:

-()cellForRowAtIndexPath{
...

cell.Plus.tag = indexPath.row;
[cell.Plus addTarget:self action:@selector(plusBtnClick:) forControlEvents:UIControlEventTouchUpInside];

...
}



-(void) plusBtnClick:(id)sender{
//update the number
}

2 个答案:

答案 0 :(得分:0)

如果您的按钮是细胞的子视图,则内容视图MyCell *cell = [[sender superview] superview]将为您提供指向包含单元格的指针。使用cell.likeLabel.text = whatEverValue

更新文字

答案 1 :(得分:0)

我必须使用标记创建NSIndexPath。请参阅下面的我在plusBtnClick函数中输入的代码。

NSIndexPath *plusIndexPath = [NSIndexPath indexPathForRow:senderButton.tag inSection:0];
CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:plusIndexPath];
cell.ScoreLabel.text = [NSString stringWithFormat:@"%ld", (long)scoreCount++];