0

我想试试,当我点击单元格时,颜色应该会改变,颜色应该留在我被点击的那个单元格上。直到这里我成功了。但是当表格刷新时,单元格背景颜色消失,我错了请帮助我,我从苹果文档中得到代码我使用这个。我错过了代码中的一些行

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

    }


}
4

1 回答 1

0

如果要在刷新时保留所选行,则可以保存所选 indexPath 并使用以下方法:

NSIndexPath *rowToSelect; //save your selected indexPath

[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone;
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNon animate:YES];
于 2012-06-05T06:23:57.993 回答