2012-06-05 67 views
0

我想嘗試,當我點擊單元格,然後顏色應該改變,顏色應該留在那個單元格,我被clicked.till在這裏,我是成功的this.But當表刷新則單元格背景顏色消失,在那裏我錯了,請幫我在這,我從蘋果文檔的代碼,我用這個。我錯過代碼如何更改單元格背景顏色或圖像上點擊ipad

- (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]; 

     //here i create view with color for cell when cell is click this view should display as background i am write or wrong by doing this please help me here why my view is disappear when my tableview is refresh then this view is disapper from cell i want to stay that place till user not click other cell&mt tableview is grouped tableview. 
     UIView *v = [[[UIView alloc] init] autorelease]; 
     v.layer.cornerRadius =6.0f; 
     v.backgroundColor = [UIColor darkGrayColor]; 
     cell.selectedBackgroundView = v; 
     } 
    return cell; 
} 



- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == 0 || indexPath.row%1 == 0) { 
     UIColor *altCellColor = [UIColor colorWithWhite:0.2 alpha:0.1]; 
     cell.backgroundColor = altCellColor; 

    } 


} 

回答

0

一些線如果你希望你的選擇留存那麼你可以保存你選擇的indexPath並使用下面的方法:

NSIndexPath *rowToSelect; //save your selected indexPath 

[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone; 
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNon animate:YES]; 
+0

嗨朋友在哪裏可以使用這段代碼我在選擇了方法的情況下進行了寫入,但是在選擇了單元格的時候它崩潰了 – Rocky

+0

您應該將您的indexPath保存在didSelect方法中,並在cellForRowAtIndexPath.and中調用上述方法,並且您能否顯示您的崩潰日誌? –

+0

我在iPad上做spliteview應用程序左視圖是tableview和右側視圖所有數據項顯示當我添加新日期時,我選擇的單元格顏色刷新並從單元中消失到現在爲止 – Rocky

相關問題