表视图 TableView 的使用

来源:互联网 发布:java 弱引用 使用场景 编辑:程序博客网 时间:2024/06/07 00:45

1、新建接口 TableViewCell ,用于显示单元格的内容

@interface RegionTableViewCell : UITableViewCell@property (strong, nonatomic) IBOutlet UILabel *regionName;@property (strong, nonatomic) IBOutlet UIImageView *regionImage;@end
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {        // Initialization code    }    return self;}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {    [super setSelected:selected animated:animated];    // Configure the view for the selected state}

2、新建 TableViewController ,作为 TableView 的控制器,并实现下面三个接口

// 返回分段数量- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}
// 返回对应分段的记录数量,即有多少行数据- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    // Return the number of rows in the section.    return self.arrRegionData.count;}


- (UITableViewCell *)tableView:(UITableView *)tableView         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获得一个单元格
    // 对单元格进行配置
}

每个返回的cell类型


4、UITableViewCell

设置单元格的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath



// 单元格发生触碰- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];  // 获得跳转时触碰的单元格索引路径}



0 0
原创粉丝点击