2011-09-08 56 views
0

我有一個表視圖,在每一行中有兩個單選按鈕在那裏,用於選擇單選按鈕需要計算節號和行號,並在當前的邏輯上到第9行它的工作正常,但第9行後它沒有改變單選按鈕圖像。iphone表視圖問題與index.section和index.row

這裏是我的一塊邏輯實現代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
cell._option1Btn.tag = ((indexPath.section+1)*10)+(indexPath.row+1); 
cell._option2Btn.tag = ((indexPath.section+1)*10)+(indexPath.row+1); 

    return cell; 
} 


-(void) respondToRightBtnAction:(UIButton*) sender { 
    if(debug)NSLog(@"In the Right button clicked method"); 
    if(debug)NSLog(@"left button in %d row is YES", sender.tag); 

    int section = (sender.tag/10 -1); 
    int row = (sender.tag%10 -1); 

}

+0

你在每個部分有多少行? – taskinoor

回答

0

我覺得問題可能出在這:

您將有相同的標籤{部分= 0,行= 10}(標籤== 21){section = 1,row = 0}(tag == 21)。嘗試改變生成標籤的方法。

例如,

cell._option1Btn.tag = ((indexPath.section+1)*1000)+(indexPath.row+1); 
cell._option2Btn.tag = ((indexPath.section+1)*1000)+(indexPath.row+1); 

係數()要小一些,然後在部分

2

它不會因爲你是你設定的標籤的方式工作的最大行數。這是一個例子。

indexPath.section = 1,indexPath.row = 1 標籤= 22

indexPath.section = 0,indexPath.row = 11 標籤= 22

一種更好的方式來做到這一點是獲取單元格本身,您可以從發件人變量中獲取單元格。

-(void) respondToRightBtnAction:(UIButton*) sender { 
    UITableViewCell *cell = sender.superview.superview //actual code will depend on the hierarchy. This assumes that the button is in the contentview of the cell 

    NSIndexPath *path = [myTableView indexPathForCell:cell]; 

    //now you can do whatever you need to do with the indexpath. 

}