0

如果单击该按钮但卡住了,我正在尝试更改动态单元格中按钮的标题:

我有:

- (IBAction)buttonWasPressed:(id)sender
{
    static NSString *CellIdentifier = @"Cell";

    NSIndexPath *indexPath =
    [self.tableView
     indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSUInteger row = indexPath.row;
    NSLog(@"row::%d",row);
    ResultsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier cellForRowAtIndexPath:indexPath];

    [cell.favoriteButton setTitle:@"favorited" forState:UIControlStateNormal];

}

它适用于我NSLog(@"row::%d",row);,但我不确定如何正确处理第二部分?有什么建议么?

4

2 回答 2

0

尝试这样的事情:

UIButton *btn = (UIButton *)sender;
[btn setTitle:@"favorited" forState:UIControlStateNormal];

正在向您的方法发送对所单击按钮的引用。你只需要使用它!

于 2013-07-29T20:54:39.563 回答
0

这就是按钮的“状态”。甚至是细胞状态。只是因为它们可能有很多按钮和不同的标题,您可以像这样配置每个按钮

[yourButton setTitle:@"Add to favourite" forState:UIControlStateDefault];
[yourButton setTitle:@"Favourite" forState:UIControlStateSelected];

然后只需设置所需的状态:

- (IBAction)buttonWasPressed:(UIButton*)sender {
    sender.selected = !sender.selected; // Toggle "selected" state
}
于 2013-07-29T21:01:42.570 回答