将UIButton标题更改为选定的表格单元格

时间:2013-06-03 12:50:01

标签: ios objective-c uitableview uibutton

我想将UIButton文本更改为UITableView中的所选单元格。通常情况下会这样做。

TableView.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  if([self.delegate respondsToSelector:@selector(ButtonText:)]) {
    [self.delegate ButtonText:selCell.textLabel.text];
  }
}

ViewController.m

- (void)dateSpecifiedButtonText:strText2 
{
    [self.dateSpecifiedButton setTitle:strText2 forState:UIControlStateNormal];   
}

当我选择单元格时这是有效的,但我想要的是当你点击另一个按钮时将按钮更改为单元格。所以我需要在不使用didSelectRowAtIndex的情况下重写此方法,但我不知道如何。

1 个答案:

答案 0 :(得分:0)

如果您知道按钮为

的单元格的indexPath
UITableViewCell *cell = [self.tableView cellForIndexPath:indexPath];

如果您刚刚向contentView的contentView添加了按钮,则为该按钮添加一个唯一标记

UIButton *button = (UIButton *)[cell.contentView viewForTag:55];

现在你有了按钮的实例,将文本设置为它。