2011-06-04 66 views
1

我有一個UITableView它看起來像這樣編程首選項面板: enter image description here的UITableView奇怪的行爲

第一部分包括在簡單的行和列中具有UISwitch作爲一個子視圖的第二部分。

當我改變景觀和開始滾動的實現代碼如下表現在陌生的路上: enter image description here

這是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
// Configure the cell 
if (indexPath.section == 0) { 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    if (indexPath.row == 0) { 
     cell.textLabel.text = @"Help"; 
    }else if (indexPath.row == 1){ 
     cell.textLabel.text = @"About us"; 
    } 
    else if(indexPath.row == 2){ 
     cell.textLabel.text = @"Send to a friend"; 
    } 
}else if(indexPath.section == 1){ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    if (indexPath.row == 0) { 
     cell.textLabel.adjustsFontSizeToFitWidth = NO; 
     cell.textLabel.text = @"Show favs at launch"; 
     [cell addSubview:favoritesSwitch]; 
    }else if (indexPath.row == 1){ 
     cell.textLabel.text = @"Open with Safari"; 
     [cell addSubview:browserSwitch]; 
    } 
    else if(indexPath.row == 2){ 
     cell.textLabel.text = @"Remember query"; 
     [cell addSubview:lastQuerySwitch]; 
    } 
    else if(indexPath.row == 3){ 
     cell.textLabel.text = @"Automatic keyboard"; 
     [cell addSubview:keyboardSwitch]; 
    } 
} 

return cell; 
} 

預先感謝您:)

回答

1

它可能會發生,因爲您使用相同的單元格標識符來訪問表格單元格。

嘗試用

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d_%d",indexPath.section,indexPath.row]; 

代替。

static NSString *CellIdentifier = @"Cell"; 
+0

謝謝:D它的工作!但是,爲什麼我的CellIdentifier不起作用? – Edu 2011-06-04 18:07:58

+0

謝謝老兄! :) – Edu 2011-06-04 18:10:38

+0

@Edu:很高興它可以幫助你。 – Jhaliya 2011-06-04 18:11:22

0

tableView正在回收您添加了browserSwitch,lastQuerySwitch和keyboardSwitch的單元。從單元格視圖中刪除這些內容沒有意義。在//配置單元格註釋之後添加以下行。我認爲這會整理一下。

[browserSwitch removeFromSuperview]; 
    [lastQuerySwitch removeFromSuperview]; 
    [keyboardSwitch removeFromSuperview]; 
4

,你應該做以下幾點:

繼續使用相同的CellIdentifier所有單元(性能,如果你創建的每個細胞,將工作一個新的標識,但消耗大量內存! )。使用多個單元標識符只適用於單元格根本不同的情況,如不同的自定義UITableViewCell子類。

請勿將您的開關添加爲子視圖(!!!),請改爲使用cell.accessoryView = mySwitch。如果這個單元不應該有一個開關,一定要設置cell.accessoryView = nil清理一個單元重用。爲您的交換機使用accessoryView將自動處理所有佈局工作。

當您將accessoryView設置爲nil時,您可以在重新使用單元格時繼續使用accessoryType作爲DetailDisclosure-Buttons!