2013-01-16 53 views
0

以下工作:我有一个表格视图,其中的项目显示在一个数组中。 每个表格视图单元格中的按钮都会返回一个布尔值。当从搜索栏搜索表视图时,如何停止index.path.row的更改?

在搜索栏中,当我搜索一个单元格时,单元格的索引从它在原表格中的值改变,这会更改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; 

} 

回答

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]; 
} 
+0

你觉得它有帮助吗? – Avigit