0

我有一张桌子,里面有 5 个单元格。每个单元格中都有一个按钮。我需要在某些情况下动态更改这些按钮。如何在表格视图中动态更改按钮。

4

3 回答 3

1

您可以做的是编写一个函数来更改按钮的值,然后您可以调用该函数。更改按钮值后,请使用以下任何一种方法:

您可以使用 :[tableview reloadData];重新加载所有表数据。

或者

您可以使用以下方法重新加载特定行:

[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];

于 2013-02-07T10:37:53.980 回答
1

您可以通过 查询所有可见单元格的表格视图-visibleCells,获取按钮的引用(假设您有一个UITableViewCell带有按钮属性的子类)并更改它们。

于 2013-02-07T10:39:09.107 回答
0

首先,您需要为每个按钮设置标签。这可以通过

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Your code
cell.Button.tag = indexPath.row;
[cell.Button addTarget:self action:@selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside];

}

然后你可以从这个方法中获取按钮

- (IBAction)buttonClick1:(id)sender {

int tagNo =[sender tag];
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]];
//Now change the action for the cell
[cellclicked.Button addTarget:self action:@selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside];

//Your Code

}

您可以按照相同的步骤 - (IBAction)buttonClick2:(id)sender

现在您可以跟踪单击了哪个按钮并更改该按钮的方法。

并声明你的 cell 和 button strong

于 2013-02-08T08:15:58.207 回答