TableView 各种设置

UITableView 默认选中一个 cell


首先定义一个变量并初始化

BOOL isSelectRow;
- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    isSelectRow = YES;
}
定义该变量是为了防止滚动UITableView时,重新选中为第一行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"ItemCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    if(isSelectRow){                                  
        // 默认选中第一行
        NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }                               
    // 设置cell元素 ...
    return cell;
}
在开始滚动是,设置isSelectRow = NO;
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    isSelectRow = NO;
}
自定义单元格背景颜色
//自定义单元格背景颜色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    cell.backgroundColor = [UIColor blackColor]; // 设置背景颜色
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-bg.png"]]; //设置选中后的背景
}
自定义UITableView的Header的高
// UITableView Header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45.0f;
}
自定义UITableView的Header
// 自定义UITableView的区段的Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //创建一个视图(_headerView)
    UIView *_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 45)];
    UIImageView *_headerImageView = [[UIImageView alloc]
                                     initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 45)];
    _headerImageView.image = [UIImage imageNamed:@"menu-heder.png"];
    [_headerView addSubview:_headerImageView];
       
    // 创建一个 _headerLabel 用来显示标题
    UILabel *_headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 16, 100, 19)];
    _headerLabel.backgroundColor = [UIColor clearColor];
    _headerLabel.textColor = [UIColor whiteColor];
    _headerLabel.font = [UIFont fontWithName:@"Arial" size:18];
   
    // 设置组的的标题
    if (section == 0) {
        _headerLabel.text = self.userModel.name;
    }
    [_headerView addSubview:_headerLabel];
       
    // 分割线
    UIImageView *_botImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 44, tableView.frame.size.width, 1)];
    _botImageView.image = [UIImage imageNamed:@"sep-bot.png"];
    [_headerView addSubview:_botImageView];
       
    return _headerView;
}
自定义UITableViewCell的分割线


在自定义的VCustomTableViewCell中的 drawRect方法中绘制:
-(void)drawRect:(CGRect)rect{
    // cell顶部-分割线
    UIImage *topImage = [UIImage imageNamed:@"sep-top.png"];
    [topImage drawInRect:CGRectMake(0, 0, self.frame.size.width, 1)];
              
    // cell底部-分割线
    UIImage *botImage = [UIImage imageNamed:@"sep-bot.png"];
    [botImage drawInRect:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值