UITableView中的部分設置

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)];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章