UITableView之显示单组数据Demo

需求

  1. UITableView实现显示单组数据。
  2. 尝试设置不同行高度不同。

效果:

在这里插入图片描述

数据展示

在这里插入图片描述

实现

  1. 与之前分组显示数据的区别在于懒加载的数据模型不同。
    (1)声明数据模型类
    类的属性一定要和plist中数据的字段保持一致
@interface CZhero : NSObject
// 图片
@property(nonatomic, copy) NSString *icon;
// info
@property(nonatomic, copy) NSString *intro;
// name
@property(nonatomic, copy) NSArray *name;

// 读取的仍然是字典
- (instancetype)initWithDict:(NSDictionary *)dict;
// 约定俗成:需要实现公有方法,类名+ WithDict
+ (instancetype)heroWithDict:(NSDictionary *)dict;

@end

(2)实现数据模型类

#import "CZhero.h"

@implementation CZhero
- (instancetype)initWithDict:(NSDictionary *)dict{
    if(self = [super init]){
        // KVC的写法:自动获取全部属性,并用字典的键赋值
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+ (instancetype)heroWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}
@end
  1. 自定义UITableView类
    (1)声明:设置行高需要声明代理,显示数据本身需Datasource
    成员:UITableView、数组
@interface UITableViewDemoOneGroup : UIView<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UITableView *uitableview;
@property(nonatomic, strong) NSArray *herodatas;

@end

(2)实现:
0> 初始化UITableView并设置dataSource、delegate。
1> 懒加载数据:单组数据和分组显示数据的格式都一样,都是从plist中加载字典,基本五个步骤。
2> 注意设置cell样式不同,UITableViewCellStyleSubtitle和StyleDefault不一样。
3> 设置奇偶行高不同。

#import "UITableViewDemoOneGroup.h"

@implementation UITableViewDemoOneGroup

-(instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        // UITableViewStylePlain:单组数据
        _uitableview = [[UITableView alloc]initWithFrame:self.bounds style: UITableViewStylePlain];
        _uitableview.dataSource = self;
        _uitableview.delegate = self;
    }
    // 开始没显示,因为没add
    [self addSubview: _uitableview];
    return self;
}

#pragma mark - 加载数据
-(NSArray *)herodatas{
    if(_herodatas == nil){
        // 懒加载: 1, 获取plist  2. 创建对象数组   3. 添加到对象数组  4. 赋值对象数组给数据group
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrayModel = [NSMutableArray array];
        // 根据字典 创建模型,加入数组
        for(NSDictionary *dict in arrayDict){
            CZhero *model = [CZhero heroWithDict:dict];
            [arrayModel addObject:model];
        }
        _herodatas = arrayModel;
    }
    return _herodatas;
}

#pragma mark - 协议相关样式
// 单组:所有英雄是一个组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

// 每组几行:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.herodatas.count;
}

// 每一行cell
// 因为只有一组,indexPath始终是0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // indexPath始终是0,所以通过row获取model
    CZhero *model = self.herodatas[indexPath.row];
    // default类型cell,不会显示简介
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.imageView.image = [UIImage imageNamed:model.icon];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.intro;
    // 单元格中显示右边的标记
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

// 设置行高:通过代理方法,也没设置self
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowNum = indexPath.row;
    if(rowNum%2 == 0){
        return 60;
    }else{
        return 100;
    }
}

@end

· 关于:设置奇偶行行高不同
如果设置所有行高度相同,用self.tableView.rowHeight属性。
但是要设置不同行的高度不同,通过代理方法实现。所以在自定义类上声明。
注意代理和数据源必须赋值self,否则不显示
如下,奇偶行宽度明显不一样
在这里插入图片描述

遇到问题

  1. 初步声明完自定义类后,运行失败

当你在自定义类中声明某协议后,一定要实现必须实现的方法。

  1. setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key intro.’

数据模型的属性名要完全和plist中的字段名完全一致。

  1. Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSCFString content]: unrecognized selector sent to instance 0x600000c68bd0’
    *** First throw call stack:

错误的赋值方式,需要使用下面方式,图片对象来赋值,而直接用图片名赋值会找不到。
[UIImage imageNamed:model.icon]

  1. UITableViewCellStyleSubtitle和UITableViewCellStyleDefault的区别:
    默认状态的cellStyle不会显示cell.detailTextLabel.text = model.intro;
    默认状态不会显示信息。
  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值