2012-06-13 176 views
1

我有其中包含多個部分的表格視圖。 我想選擇的只有一排...我的tableview的選擇屬性被設置爲允許:SingleSelectioin在tableview中取消選擇先前選擇的單元格

現在我這樣做是爲了設置複選標記選定行

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) 

{ 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

else 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

現在我想那是什麼當我選擇任何其他單元格時,先前選擇的單元格應該設置回未選中狀態。

我試圖與這

lastIndexpath=indexPath; 

然後

[tableView deselectRowAtIndexPath:lastIndexpath animated:YES]; 

,但它使我對lasIndexPath

EXC_BAD_ACCESS (code=1,address= 0xfe000008) 

壞接入請幫助我這個

任何新建議也歡迎

+0

您是否在使用ARC?如果不嘗試:[lastIndexpath release]; lastIndexpath = [indexPath retain]; – iTukker

+0

是的現在它不是給我BAD_ACCESS,但我想要的是,一次只應選擇一行 – Anand

+0

你能檢查以下嗎? lastIndexpath = indexPath.row; –

回答

3

要標記所選單元格,然後在didSelectRowAtIndexPath方法,你可以這樣寫代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

     cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedIndexPath]; 
     [cell setAccessoryType: UITableViewCellAccessoryNone]; 

    if(previousSelectedIndexPath!=nil) 
    { 
     [previousSelectedIndexPath release]; 
     previousSelectedIndexPath = nil; 
    } 
    previousSelectedIndexPath = [indexPath retain]; 
} 

其中previousSelectedIndexPath以前選擇指數;

+0

什麼是previousSelecedIndexPath以及如何爲其分配值 – Anand

+0

它與您的lastIndexpath相同,即之前選擇的indexPath。 –

2

聲明NSIndexPath * lastIndexpath在.H控制器的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(lastIndexpath){ 
    if([tableView cellForRowAtIndexPath:lastIndexpath].accessoryType == UITableViewCellAccessoryCheckmark) 

{ 

    [tableView cellForRowAtIndexPath:lastIndexpath].accessoryType = UITableViewCellAccessoryNone; 
} 
else 
{ 
    [tableView cellForRowAtIndexPath:lastIndexpath].accessoryType = UITableViewCellAccessoryCheckmark; 

}} 
    lastIndexpath=[indexPath retain] 
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 
else 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 
} 
1
//Take a variable of type indexpath in .h 

NSIndexPath *myIndexPath 

//Now check it in your cellForRowAtIndexPath: delegate method .. 

if(indexPath == myIndexPath) 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
else 
    cell.accessoryType = UITableViewCellAccessoryNone; 

//and in your didSelect: Delegate method ... 

[tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark 
myIndexPath = indexPath; 
[tableView reloadData]; 

希望這會幫助你。

1

在委託方法willSelectRowAtIndexPath中,獲取當前選定的索引路徑,將accerroryType設置爲none。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow]; 

    [tableView cellForRowAtIndexPath:currentSelectedIndexPath].accessoryType = UITableViewCellAccessoryNone; 

    if ([indexPath isEqualTo: currentSelectedIndexPath]) 
    { 
     return nil; 
    } 
    return indexPath; 
} 
+0

謝謝大家,我的問題得到解決只是通過保留和使用lastIndexpath ... – Anand

相關問題