Objective-C 学习记录 - 20

1.UITableView 展示数据的方法
需要设置UITableViewDataSource协议,UITableView会自动向dataSource请求数据

self.tableView.dataSource = self;

/** 告诉tableView一共有多少组数据 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

/** 告诉tableView第section组有多少行 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    } else {
        return 6;
}

/** 告诉tableView每一行显示的内容(tableView每一行都是UITableViewCell或者它的子类) */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] init];
    if (indexPath.section == 0){  //第0组
        if (indexPath.row == 0){  //第0行
            cell.textLabel.text = @“要显示的文字”;  //文字
            cell.imageView.image = [UIImage imageNamed:@“图片的名字”];  //图片
            cell.accessoryType = UITableViewCellAccessoryCheckmark;  //设置cell右边的指示样式
            cell.accessoryView = [[UISwitch alloc] init];  //设置cell右边显示的控件,优先级大于accessoryType
        }
    }
    return cell;
}

/** 告诉tableView每一组的头部标题 */
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @“第一组的头部标题”;
    } else {
        return @“其他组的头部标题”;
    }
}

/** 告诉tableView每一组的尾部标题 */
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return @“第一组的尾部标题 ”;
    } else {
        return @“其他组的尾部标题 ”;
    }
}

猜你喜欢

转载自blog.csdn.net/XtheEpic/article/details/81813485
今日推荐