iOS大典之表视图UITableView


UITableView


表视图用来显示数据列表,


它遵守两个协议:

因为

获取配置数据 UITableViewDelegate
获取行数据 UITableViewDataSource


表视图有两种基本样式:
1: 无格式表 UITableViewStylePlain .
每个分组的标题可以自定义样式, 使用了索引, 又叫索引表
2: 分组表 UITableViewStyleGrouped .
分组间有明显的间距, 一个分组表只有一个分组, 分组表不该使用索引


表视图单元格样式:

UITableViewCellStyleDefault, 不含详细文本

UITableViewCellStyleValue1, 单元两端, 详细在后

UITableViewCellStyleValue2
不显示图标, 详细文本标签在文本标签的左侧

UITableViewCellStyleSubtitle 一上一下, 详细在下


创建后先设置协议

@interface ViewController ()<UITableViewDataSource, UITabBarDelegate>

一般会用数组来把数据显示出来

@property (copy, nonatomic)NSArray *fuck;
- (void)viewDidLoad {
    [super viewDidLoad];

    self.fuck = @[@"A", @"F", @"C", @"U", @"K"];
    UITableView *tableView = (id)[self.view viewWithTag:1];
    UIEdgeInsets contentInset = tableView.contentInset;
    contentInset.top = 20;
    [tableView setContentInset:contentInset];
}

最重要的两个属性, 必不可少

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dwarves count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     // 声明一个静态字符串
    static NSString *FuckIdentifier = @"FuckIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FuckIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:FuckIdentifier];
     // 如果是nil, 就用标识符字符串手动创建个新的表视图单元
 }

    cell.textLabel.text = self.dwarves[indexPath.row];
     //  从indexPath的row属性获取当前行, 用表的行号从数组中获得对应的字符串, 然后赋给表单元的textLabel.text属性, 最后将表单元返回
    return cell;
}

loading


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值