IOS UITableView总结

来源:互联网 发布:数据集成工具 编辑:程序博客网 时间:2024/05/18 03:02

UITableView需结合UITableView的两个代理协议共同使用  <UITableViewDelegate,UITableViewDataSource>

在定义UITableView时,需同时设置其代理

    tableView.delegate = self;    tableView.dataSource = self;


一、
UITableView有两种类型,在初始化时就可以设置(下面这个是UITableView的初始化函数):

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER; // must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain

    UITableViewStylePlain,          // regular table view    UITableViewStyleGrouped         // preferences style table view
一种是普通类型(如IOS里面的便笺),另一种是分组类型(如IOS里面的设置)

二、UITableView的协议函数

1.设置UITable的组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

2.设置UITableView每组的个数

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

3.设置每组的头部(尾部)名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section


4.设置头部(尾部)的高度

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

5.给头部(尾部)添加视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section


6.给每一个cell添加内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    注意:在每一个cell里面,可以引用这样的格式,然后再设置他的视图(文本),最后返回cell

    NSString *strID = @"ID";    //尝试获取可以复用的单元格    //如果得不到,返回nil    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:strID];    if (cell == nil)    {        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];    }


7.自动调整自视图的大小

@property(nonatomic) UIViewAutoresizing autoresizingMask;

8.设定进行重新加载的函数

- (void)reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks

9.UITableView设置数据心得

    如果有多组数据,可以使用字典的方式把字符串和array联系在一起!!!








原创粉丝点击