iOS控件之UITableView

  UITableView是iOS中最常用的控件的一种,UITableView的风格有两种:UITableViewStylePlain 和 UITableViewStyleGroup,因为UITableViewStyle 的属性为只读属性,所以我们要在初始化的时候指定其风格是Plain还是Group。UITableView的基本元素是单元格,也就是UITableViewCell,我们所看到的UITableView就是由一个个UITableViewCell组成的。下面我们来介绍一下UITableViewCell的设定和自定义,做出一个符合项目要求的UITableView。

(一)、创建一个空的UItableView的项目:

            首先创建一个工程,然后创建一个UIViewController,在UIViewController的m 文件的viewDidLoad中的代码如下:

//    初始化tableView并且给定style
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame
                                                         style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
//    设置tableView的单元格属性,每一列的高度,每一组的头和尾的高度
    tableView.rowHeight = 100;
    tableView.sectionHeaderHeight = 30;
    tableView.sectionFooterHeight = 30;
    [self.view addSubview:tableView];

(二)、UITableView的dataSourse和delegate:

  UITableView的dataSourse负责UITableViewCell的数据显示以及Cell的显示样式,系统默认的Cell的样式有四种:UITableViewCellStyleDefault、UITableViewCellStyleValue1、UITableViewCellStyleValue2 、UITableViewCellStyleSubtitle。系统默认Cell显示出来的几种属性cell.textLabel 、cell.imageView 、cell.detailTextLabel、cell.accessoryType。UITableView的delegate是Cell相关的响应方法。首先我们要先让UIViewController遵守UITableViewDataSourse和UITableViewDelegate,设置tableView.delegate = self;tableView.dataSource = self;

(a)、UITableViewCellStyleDefault只显示cell.textLabel、cell.imageView、cell.accessoryType;具体显示样式如下图:

(b)、UITableViewCellStyleValue1具体显示如图:

(C)UITableViewCellStyleValue2的显示风格:

(d)、UITableViewCellStyleSubtitle的显示样式:

UITableViewCellDataSourse中有两个是必须要实现的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

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

系统提供的这四种样式能满足一般工程的要求,根据所需求不同进行相应的设置,以最后一种为例,示例代码如下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listNames.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indetifier];
        
    }
    cell.textLabel.text = self.listNames[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"icon_company_orange"];
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 20;
    cell.detailTextLabel.text = @"ksdjfsfj";
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    return cell;
    
}

(四)、自定义Cell:

如果以上系统样式的Cell不满足需要,或者不能给用户更好的体验,那么iOS给我们提供了自定义Cell,在自定义cell.contentView上添加或者布局我们想要的Cell的显示风格和样式。首先创建一个继承自UIViewControllerCell的类,然后在其init方法中重构其内容,代码如下:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _labelUpdateTime = [[UILabel alloc] initWithFrame:CGRectMake(250, 5, 60, 40)];
        _labelUpdateTime.font = [UIFont systemFontOfSize:14.0];
        //如果要自定义Cell单元格,则需要通过UITableViewCell的contentView为添加
        [self.contentView addSubview:_labelUpdateTime];

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(170, 40, 60, 20)];
        view.backgroundColor = [UIColor darkGrayColor];
        [self.contentView addSubview:view];

    }
    return self;
}

然后用我们自己设置好的类的对象对UITableView进行设定。

以上是我自己对UITableView的学习和理解。

原文地址:https://www.cnblogs.com/zhaopengtao14/p/3757816.html