我想尝试,当我点击单元格然后颜色应该改变,颜色应该保持在我被点击的那个单元格。在这里我是成功的。但是当表刷新时,单元格背景颜色消失在哪里我错了请帮助我这个我从苹果文档中得到代码我用这个。我错过了一些代码行
- (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 :(得分:0)
如果要在刷新时保留所选行,则可以保存选定的indexPath并使用以下方法:
NSIndexPath *rowToSelect; //save your selected indexPath
[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone;
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNon animate:YES];