iOS UITableView 设置默认选中行及其颜色

来源:互联网 发布:小米抢购软件app 编辑:程序博客网 时间:2024/06/05 03:39

//在创建UITableView或获取数据的地方设置默认选中一行,这里默认选中第一行

    [self.tableViewselectRowAtIndexPath:[NSIndexPathindexPathForItem:0inSection:0]animated:YESscrollPosition:UITableViewScrollPositionTop];

    if ([self.tableView.delegaterespondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {

        [self.tableView.delegatetableView:self.tableViewdidSelectRowAtIndexPath:[NSIndexPathindexPathForItem:0inSection:0]];

    }


//设置选中颜色是在以下执行的

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    staticNSString * cellId =@"SortTableViewCell";

    UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:cellId];

    if (cell ==nil) {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];

    }

    

    cell.selectedBackgroundView = [[UIViewalloc] initWithFrame:cell.frame];

    cell.selectedBackgroundView.backgroundColor =[UIColorwhiteColor];

    

    return cell;

}



阅读全文
0 0
原创粉丝点击