簡體   English   中英

如何在ipad中單擊更改單元格背景顏色或圖像

[英]how to change cell background color or image on click in 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;

    }


}

如果要在刷新時保留選定的行,則可以保存選定的indexPath並使用以下方法:

NSIndexPath *rowToSelect; //save your selected indexPath

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM