简体   繁体   中英

how to change cell background color or image on click in ipad

i want to try ,when i click on cell then color should change and color should stay on that cell which i was clicked.till here i was success in this .But when the table is refresh then cell background color is disappear where i am wrong please help me on this i got code from apple document i use this .I miss some line in code

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

    }


}

If you want to retained your selected row on refresh then you can save your selected indexPath and use following method:

NSIndexPath *rowToSelect; //save your selected indexPath

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM