UITableView 默认选中一个 cell

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

1
2
3
4
5
6
BOOL isSelectRow;
- ( void )viewDidLoad{
     [ super viewDidLoad];
     // Do any additional setup after loading the view.
     isSelectRow = YES ;
}

定义该变量是为了防止滚动UITableView时,重新选中为第一行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-(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;

1
2
3
-( void )scrollViewWillBeginDragging:(UIScrollView *)scrollView{
     isSelectRow = NO ;
}

自定义单元格背景颜色

1
2
3
4
5
//自定义单元格背景颜色
- ( 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的高

1
2
3
4
// UITableView Header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:( NSInteger )section{
     return 45.0f;
}

自定义UITableView的Header

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 自定义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方法中绘制:

1
2
3
4
5
6
7
8
9
-( 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)];
}