2010-08-11 62 views
0

我有一個selectedArray定義爲我的文件中的一個屬性,它保存哪些單元格已被「檢查」。當顯示單元格時,我檢查當前單元格值是否在selectedArray中,如果是,則顯示UITableViewCellAccessoryCheckmark作爲表格的附件視圖。UITableView不適當刷新

唯一的問題是,當用戶使用UISearchBar通過UITableView進行搜索時,我會要求表reloadData完成搜索後,並且在numberofRows方法中,我會在selectedArray中看到適量的對象。但是,cellForRowatIndexPath方法仍會顯示舊複選標記,就好像selectedArray從未更新過一樣。

任何人都可以幫我嗎?這是我的代碼 -

NSArray *tempArray = [[NSArray alloc] init]; 
if(tableView == theTable) { 
    tempArray = [contactsArray objectAtIndex:indexPath.row]; 
} else { 
    tempArray = [searchfor_array objectAtIndex:indexPath.row]; 
} 

NSString *cellValue = [tempArray objectAtIndex:0]; 
static NSString *CellIdentifier; 
CellIdentifier = cellValue; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)]; 
    name.text = [tempArray objectAtIndex:0]; 
    name.backgroundColor = [UIColor clearColor]; 
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
    [cell.contentView addSubview:name]; 
    [name release]; 

    if([selectedArray containsObject:[tempArray objectAtIndex:0]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

} 

return cell; 
[cell release]; 
[tempArray release]; 
[cellValue release]; 

回答

0

添加到Justin的答案 -

你應該在假如是添加標籤(細胞==零)僅部分。您處理該陳述之外的附件視圖。下面是它應該是什麼樣子 -

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)]; 
    name.text = [tempArray objectAtIndex:0]; 
    name.backgroundColor = [UIColor clearColor]; 
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
    [cell.contentView addSubview:name]; 
    [name release]; 


} 

if([selectedArray containsObject:[tempArray objectAtIndex:0]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
+0

感謝您填補我答案中的空白。 – Justin 2010-08-11 12:01:22

1

它看起來好像你設置單元格一次並重新使用它。單步執行代碼,您應該看到if塊僅在單元格第一次加載時進入。

你要調整單元格的每一行,像這樣:

if (cell == nil) { 
    // Initialize cell. 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Adjust cell for each row. 
if([selectedArray containsObject:[tempArray objectAtIndex:0]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
+0

當我做到這一點,雖然,它並沒有解決問題,標籤開始彼此堆疊起來...... – 2010-08-11 03:18:55

+0

我沒有詳細介紹如何設置單元格,錯過了其他的東西來清除輔助視圖。拉斐爾已經把它覆蓋了。 – Justin 2010-08-11 12:05:18