[英]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.