美文网首页iOS DeveloperiOS学习
iOS UITableView中cell的自带样式

iOS UITableView中cell的自带样式

作者: 暗尘随码去 | 来源:发表于2016-11-22 15:07 被阅读0次

UITableView是iOS开发中用的非常多的一个控件,UITableview用来展示列表数据,相当于安卓中的listview。不同于安卓中的listview自定义item来定义列表显示的样式,iOS中系统自带了四种样式在很多场合都够我们使用了。

系统自带的UITableView样式有两种:

  • UITableViewStylePlain:(展示单组数据,没有分组)
UITableViewStylePlain.png
  • UITableViewStyleGrouped:(展示分组数据)
UITableViewStyleGrouped.png

系统自带的UITableViewCell样式有4种:

  • UITableViewCellStyleDefault:
    • Default样式:左边有一个显示图片的imageView和一个标题textLabel。
    • 上面是头布局显示一张图片(可做成大部分APP中下拉时图片有拉伸且粘在顶部的效果)
    • 使用代码:为了提高效率我直接对字典取值显示(下面都是)
static NSString * cellID=@"cellID";
   UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
   if (cell==nil) {
       cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
   }
   cell.textLabel.text = self.dataArr[indexPath.row][@"title"];
   cell.imageView.image = [UIImage imageNamed:@"test_list"];
//cell右侧的小箭头
   //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   return cell;
UITableViewCellStyleDefault.png
  • UITableViewCellStyleSubtitle:
    • Subtitle样式:左边还是一个显示图片的imageView,不同的是上边有一个主标题textLabel和一个副标题detailTextLabel。主标题字体大且加黑,副标题字体小在主标题下边。
    • 代码使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"Name"];
    cell.detailTextLabel.text = self.dataArr[indexPath.row][@"Others1"];
    cell.imageView.image = [UIImage imageNamed:@"music"];
    cell.backgroundColor = LightWrite;
    return cell;
}
UITableViewCellStyleSubtitle.png
  • UITableViewCellStyleValue1:
    • Value1样式:左边显示图片的imageView和一个主标题textLabel,右边一个副标题detailTextLabel。
    • 代码使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"Name"];
    cell.detailTextLabel.text = self.dataArr[indexPath.row][@"Others1"];
    cell.imageView.image = [UIImage imageNamed:@"music"];
    cell.backgroundColor = LightWrite;
    return cell;
}
UITableViewCellStyleValue1.png
  • UITableViewCellStyleValue2:
    • Value2样式:左边一个主标题textLabel字体偏小,右边一个副标题detailTextLabel。
  • imageView可选(显示在最左边)
  • 代码使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"Name"];
    cell.detailTextLabel.text = self.dataArr[indexPath.row][@"Others1"];
    cell.imageView.image = [UIImage imageNamed:@"music"];
    cell.backgroundColor = LightWrite;
    return cell;
}
UITableViewCellStyleValue2.png

当然如果如果你想要在每个cell的右侧添加一个小箭头来提示用户cell可点 只需要加上这句代码即可

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

虽然系统自带了4种样式,不过平时开发中更多的还是自定义cell的样式来满足需求!

相关文章

网友评论

    本文标题:iOS UITableView中cell的自带样式

    本文链接:https://www.haomeiwen.com/subject/llwppttx.html