Ios 在第一次加载表视图时更改选定行的背景色
您好,我正在开发一个小的IOS应用程序,我正在tableview中加载一些数据。所以我想做的是,我想让第一行作为默认的选定行,带有一些选定的背景色。所以我试着做一些事情Ios 在第一次加载表视图时更改选定行的背景色,ios,iphone,uitableview,selecteditem,Ios,Iphone,Uitableview,Selecteditem,您好,我正在开发一个小的IOS应用程序,我正在tableview中加载一些数据。所以我想做的是,我想让第一行作为默认的选定行,带有一些选定的背景色。所以我试着做一些事情 // inside viewDidLoad make first row as selected row NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; [_tableView selectRowAtIndexPath:inde
// inside viewDidLoad make first row as selected row
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:0];
// inside cellForRowAtIndexPath set some selection color
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [customColor colorWithHexString:@"91979755"];
[cell setSelectedBackgroundView:bgColorView];
}
但它不起作用。有人知道怎么做吗?我需要一些帮助。谢谢。尝试此代码,将其放入cellForRow方法:
self.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[_tabelView selectRowAtIndexPath:self.indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
// inside cellForRowAtIndexPath set some selection color
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.indexPath.row==indexPath.row){
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [customColor colorWithHexString:@"91979755"];
[cell setSelectedBackgroundView:bgColorView];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.indexPath=indexPath;
}
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cyanColor];
[cell setSelectedBackgroundView:bgColorView];
对于特定行,将其置于if条件下
希望它能起作用。尝试此委托方法-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)rowatindexpath:(nsindepath*)indepath{//NSInteger totalRow=[tableView numberofrowsinssection:indepath.section];//首先通过当前indepath获取该节中的行总数。if(indepath.row==0){//这是部分的最后一行。//cell.accessoryType=UITableViewCellAccessoryDetailDispositionButton;cell.backgroundColor=[UIColor redColor];}您好,您的解决方案对我有效。但此解决方案的唯一问题是,一旦我滚动我的表视图,它将显示选定的其他行。