0

以下作品:我有一个表格视图,其中包含从数组中显示的项目。每个表格视图单元格中的按钮返回一个布尔值。

在搜索栏中,当我搜索单元格时,单元格的索引从原始表中的值更改,这会更改indexPath.row并且我的按钮indexPath在单击时使用不正确。

我的按钮中的相关代码:

(void)clickedButton:(UIButton*)sender {

NSLog(@"Row: %d", sender.tag);

NSIndexPath* indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Stories *story = (Stories *)[self.fetchedResultsController objectAtIndexPath:indexPath];
BOOL isSaved = [story.isBookmarked boolValue];
story.isBookmarked = [NSNumber numberWithBool:!isSaved];

我的相关代码cellForRowAtIndexPath

if (tableView == self.searchDisplayController.searchResultsTableView){

     [cell.button setSelected:[story.isBookmarked boolValue]];
    cell.button.tag = indexPath.row;

    }

else
{

         [cell.button setSelected:[story.isBookmarked boolValue]];
    cell.button.tag = indexPath.row;

}
4

1 回答 1

0

indexPath.row可能会根据您的搜索结果而改变。尝试此代码以获取indexPath.

(void)clickedButton:(UIButton*)sender {
    UIButton *button = (UIButton *)sender;
    // Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
    UITableViewCell *cell = (UITableViewCell*)[[button superview] superview];

    NSIndexPath* indexPath = [subListTable indexPathForCell:cell];
}
于 2013-01-16T16:59:53.517 回答