iOS UI入门——使用Objective-C和Swift简单实现UITableView

Objective-C代码:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView * tableView;

@property(nonatomic,strong) NSArray * dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self initData];
}

-(void)initData{
    self.dataArray = @[@"我的歌声里",@"子之于归",@"划地为牢",@"故人叹",@"锦鲤抄"];
    [self.tableView reloadData];
}

#pragma mark - tableViewDelegate & tableViewDataSource
//设置section的数量,该方法不是必须实现的方法,默认返回1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//设置section下的行数,此方法为必须实现的方法,如果设置了DataSource但没实现这个方法,程序会崩溃
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

//设置每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

//设置每个section头部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

//设置每个section尾部的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

/**
 设置每行的显示内容,此方法为必须实现的方法,如果设置了DataSource但没实现这个方法,程序会崩溃
 简单的cell样式可以直接使用UITableViewCell类来处理
 复杂的cell样式,可以自定义cell类,继承自UITableViewCell类,然后自定义UI即可
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //设置cell的复用
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        //初始化cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
        //设置每行在被选中时的样式
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        //设置右边显示样式
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //设置cell默认的Label
    cell.textLabel.text = self.dataArray[indexPath.row];
    //显示cell
    return cell;
}

//设置头部
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    view.backgroundColor = [UIColor orangeColor];
    return view;
}

//设置尾部
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    view.backgroundColor = [UIColor greenColor];
    return view;
}

//处理每行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

//设置左滑编辑功能
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//处理左滑事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

}

#pragma mark - lazy
-(UITableView *)tableView{
    if (!_tableView) {
        //初始化
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        //设置每行分割线的样式
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        //设置代理
        _tableView.delegate = self;
        //设置数据源代理
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

开发过程中我们可以封装一个含有tableView的ViewController,在基础类中完成基本设置,方便tableView的使用。封装代码可参考https://blog.csdn.net/aaaaazq/article/details/79883062

Swift代码:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.addSubview(self.tableView)
    }

    //设置section的数量,默认返回1
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    //设置section下的行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }

    //设置每行的高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    //设置每个section头部的高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }

    //设置每个section尾部的高度
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 30
    }

    /**
     设置每行的显示内容,此方法为必须实现的方法,如果设置了DataSource但没实现这个方法,程序会崩溃
     简单的cell样式可以直接使用UITableViewCell类来处理
     复杂的cell样式,可以自定义cell类,继承自UITableViewCell类,然后自定义UI即可
     */
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //设置cell的复用
        var cell = tableView.dequeueReusableCell(withIdentifier: "ID")
        if cell == nil {
            //初始化cell
            cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "ID")
            //设置每行在被选中时的样式
            cell?.selectionStyle = UITableViewCellSelectionStyle.blue
            //设置右边显示样式
            cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        }
        //设置cell默认的Label
        cell?.textLabel?.text = self.dataArray[indexPath.row] as? String
        //显示cell
        return cell!
    }

    //设置头部
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 30))
        view.backgroundColor = UIColor.orange
        return view

    }

    //设置尾部
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 30))
        view.backgroundColor = UIColor.red
        return view
    }

    //处理每行的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }

    //设置左滑编辑功能
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    //处理左滑事件
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    }

    //懒加载
    lazy var tableView: UITableView = {
        let testTbaleView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.grouped)
        testTbaleView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        testTbaleView.delegate = self
        testTbaleView.dataSource = self
        return testTbaleView
    }()

    lazy var dataArray: NSArray = {
        return ["我的歌声里","子之于归","划地为牢","故人叹","锦鲤抄"]
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

刚开始写Swift,说实话关于!和?用的还不是很6,不过对于Swift,感觉xcode支持的很好,很多警告和错误都提供了fix,后面语法那一块我也会再去深入看看的,现在写代码方便入手,毕竟干看语法比较枯燥也吸收的比较慢。
tableView比较常用,我这个写的比较简单,后面我再写一下自定义cell的例子。

猜你喜欢

转载自blog.csdn.net/aaaaazq/article/details/80856278