"IOS"空件TableView中,字典,数组的套用........

本文介绍如何在TableView中使用字典和数组嵌套的方法来组织数据,包括创建多级数组结构并将其与TableView结合使用的过程。同时展示了如何实现单元格的插入、删除操作以及元素的交换等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TableView中,字典,数组的套用........

例:在大数组中建立多个小数组,每个小数组中包含着字典........

_tableArray = [[NSMutableArray alloc] init];

NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

[dic1 setObject:@"1.jpg" forKey:@"imageName"];

[dic1 setObject:@"刘大" forKey:@"name"];

[dic1 setObject:@"" forKey:@"sex"];


NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];

[dic2 setObject:@"1.jpg" forKey:@"imageName"];

[dic2 setObject:@"王二" forKey:@"name"];

[dic2 setObject:@"" forKey:@"sex"];


NSMutableArray *array = [NSMutableArray array];

[array addObject:dic1];//将字典放入小数组中

[_tableArray addObject:array];//再将小数组放入大字典中

        

NSMutableArray *array2 = [NSMutableArray array];

[array addObject:dic2];

[_tableArray addObject:array2];


TableView中的其他空件....

//增加或者删除状态

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    //return UITableViewCellEditingStyleDelete;//删除

    return UITableViewCellEditingStyleInsert;//增加

}


reloadData//tableView所有的协议重新走一遍


//编辑"删除"按钮文字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"Very Good";//按钮改由delete改变成“Very Good”

}

//交换元素,并保存元素交换后的位置

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //[_tableArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];//只是交换了数组的元素

    id value = [[_tableArray objectAtIndex:sourceIndexPath.row]retain];

    NSLog(@"value1 == %d",[value retainCount]);

    [_tableArray removeObjectAtIndex:sourceIndexPath.row];

    NSLog(@"value2 == %d",[value retainCount]);

    [_tableArray insertObject:value atIndex:destinationIndexPath.row];

    NSLog(@"value3 == %d",[value retainCount]);

    [value release];

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值