iOS 不固定数据列表的几个做法

来源:互联网 发布:mac中文件夹无法删除 编辑:程序博客网 时间:2024/05/26 22:07

  • 1设置row高度为0
  • 2筛掉数组中的空数据
  • 3使用runtime初始化数据


项目中经常遇到下面的界面,表单中有很多信息,如:电话号码、固定电话、邮件、传真、家庭地址、公司地址、部门、职位、网址等等。
如果对应的数据没有,则不显示。

图片

虽然问题并不复杂,还是在这里整理下。大致用了下面几个做法:


1、设置row高度为0

即:当这个indexPath对应的detail数据为空时,heightForIndex 高度设置为0即可。


2、筛掉数组中的空数据

初始化数据的时候,判断数据为空,则不添加到tableView的数据源数组。注意:不能边遍历,边删除。

如果有对应的图片数组imgArray,则使用for循环同时遍历两个数组。


3、使用runtime初始化数据

在拿到上述个人数据时,我们会保存到一个模型。如保存到 PersonModel 和对应的 phone,address等属性中。
然后把属性赋给数组 detailArray = @[model.phone,model.address…]中。

但如果这个数据tableView 出现在多个控制器。只能把这个detailArray 复制粘贴了。

是否可以把每一项作为一个item,item中包含图片名、标题、对应模型的属性名呢?

所以我的第三种做法是:
1、创建item

@interface CardListItem : NSObject@property (nonatomic, copy) NSString *itemIconName; //图标名@property (nonatomic, copy) NSString *itemName;@property (nonatomic, copy) NSString *propertyName;@property (nonatomic, copy) NSString *itemDetail; //item详情+ (instancetype)itemWithIconName:(NSString *)iconName propertyName:(NSString *)propertyName;@end

2、创建item项:
把对应icon和属性名赋值给item

+ (NSArray *)getCardListItems{    CardListItem *item0 = [CardListItem itemWithIconName:@"" propertyName:@""];    CardListItem *item1 = [CardListItem itemWithIconName:@"" propertyName:@""];    CardListItem *item2 = [CardListItem itemWithIconName:@"" propertyName:@""];    CardListItem *item3 = [CardListItem itemWithIconName:@"" propertyName:@""];   // ...    NSArray *listItems = @[item0,item1,item2,item3,                                  item4,item5,item6,item7,                                  item8,item9,item10,item11,];    return listItems;}

3、根据属性名 获取属性值

#pragma mark -根据属性名 获取模型实例的属性值+ (NSString *)getValueWithProperty:(NSString *)targetProperty OfModel:(id)model{        NSMutableDictionary *propDict = [NSMutableDictionary dictionary];        unsigned int outCount, i;        objc_property_t *properties = class_copyPropertyList([model class], &outCount);        for (i = 0; i < outCount; i++) {            objc_property_t property = properties[i];            //获得属性名,如 name            const char *propertyName = property_getName(property); //属性名            NSString *propertyNameStr = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];            if ([targetProperty isEqualToString:propertyNameStr]) {                //获得实例属性值                id propertyValue = [model valueForKey:propertyNameStr];                NSString *propertyValueStr = [StringTools getStringWithID:propertyValue];                return propertyValueStr;            }        }    return @"";}

4、根据属性值是否为空,创建需要展示的item数组

+ (NSArray *)listWithCardDataModel:(CardDataModel *)dataModel{    NSMutableArray *listArray = [NSMutableArray array];    NSArray *listItems = [CardTools getCardListItems];    for (CardListItem *item in listItems) {        item.itemDetail = [CardTools getValueWithProperty:item.propertyName OfModel:dataModel];        if (![Tools isBlankString:item.propertyName]) {            [listArray addObject:item];        }    }    return listArray;}

缺点:
1、一个界面本来只需要两个对象:iconArray 和 detailArray,但这样就用了多个 model的数组。
2、获取模型的属性值,是用的遍历的方法,耗时长。不如在detailArray 数组中使用 model.name 的指针来得快。

优点:
可用于多个界面,不需要每次都创建数组。