讲解iOS开发中UITableView列表设计的基本要点

2020-01-14 19:26:15于海丽
复制代码
@interface general_table_view ()<UITableViewDataSource,UITableViewDelegate>
 
@end
③在.m中的initWithFrame方法内部设置table的代理
复制代码
// Initialization code
        self.delegate   = self;
        self.dataSource = self;
以及添加tableViewFrame的set方法
复制代码
-(void)setTableViewFrame:(CGRect)tableViewFrame
{
    self.frame = tableViewFrame;// 设置tableView的frame为所传值
}
④接下来实现tableView的dataSource和delegate方法

 

必须实现的方法有两个

复制代码
// tableView每个分区的行数,可以为各个分区设置不同的行数,根据section的值判断即可
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_textLabel_MArray count];
}
 
// 实现每一行Cell的内容,tableView重用机制
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 为其定义一个标识符,在重用机制中,标识符非常重要,这是系统用来匹配table各行cell的判断标准,在以后的学习中会体会到
    static NSString *cellIdentifier = @"cellIdentifier";
     
    // 从缓存队列中取出复用的cell
    UITableViewCell *cell           = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     
    // 如果队列中cell为空,即无复用的cell,则对其进行初始化
    if (cell==nil) {
         
        // 初始化
        cell                    = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];