IOS详解TableView——静态表格使用以及控制器间通讯

本文介绍了一个iOS应用中的个人资料创建、读取、更新和删除(CRUD)功能及搜索实现。通过使用代理模式处理视图控制器间的数据通讯问题。

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

文章来源:http://blog.youkuaiyun.com/cocoarannie/article/details/11822607

一个Demo 通过使用静态表格来完成个人资料增加编辑以及搜索。不过通常我们会使用更灵活的Group风格表视图来完成。

上篇文章简单的介绍了一下搜索框的使用。这里给其加入数据来说明。


先看下效果



主要涉及到三个页面,列表页面,显示信息页面,以及添加/编辑界面。

主要解决的就是视图控制器间的数据通讯问题,以及在搜索表格点击后更改数据同样能进行正确地传递。这里的数据通讯采用了比较常用的代理设计模式。


storyboard



个人信息储存在一个模型类Person中


[cpp]  view plain copy
  1. @interface Person : NSObject <NSCoding>  
  2.   
  3. @property (strong, nonatomic) NSString *name;  
  4. @property (strong, nonatomic) UIImage *headerImage;  
  5. @property (strong, nonatomic) NSString *qq;  
  6. @property (strong, nonatomic) NSString *sex;  
  7. @property (strong, nonatomic) NSString *birthday;  
  8. @property (strong, nonatomic) NSString *signature;  
  9.   
  10. @end  

遵守了NSCoding 可以对其信息进行归档解档。


为了能正确显示搜索框的内容,需要对表视图的数据源和代理方法对tableView进行判断,比如表格视图显示的内容


[cpp]  view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString * const CellIdentifier = @"PersonCell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  5.     if (!cell)  
  6.     {  
  7.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];  
  8.     }  
  9.     Person *p;  
  10.     if (tableView == self.searchDisplayController.searchResultsTableView)  
  11.     {  
  12.         p = _resultList[indexPath.row];  
  13.     }  
  14.     else  
  15.     {  
  16.         p = _personList[indexPath.row];  
  17.     }  
  18.   
  19.     cell.textLabel.text = p.name;  
  20.     cell.detailTextLabel.text = p.signature;  
  21.     cell.imageView.image = p.headerImage;  
  22.       
  23.     return cell;  
  24. }  


列表页面上点击加号与点击Cell进入不同的页面



所以要判断并给Segue设置标识符


[cpp]  view plain copy
  1. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  
  2. {  
  3.     if ([segue.identifier isEqualToString:@"AddSegue"])  
  4.     {  
  5.         EditViewController *evc = (EditViewController *)segue.destinationViewController;  
  6.         evc.editDelegate = self;  
  7.     }  
  8.     if ([segue.identifier isEqualToString:@"InfoSegue"])  
  9.     {  
  10.         _infoVC = (InfoViewController *)segue.destinationViewController;  
  11.         _infoVC.infoDelegate = self;  
  12.     }  
  13. }  

注意到这里把两个视图控制器的Delegate都设给了自己,主要是为了在编辑和增加过后,能把数据模型的信息发送给首页的试图控制器,让其刷新数据。

来看下两个协议


[cpp]  view plain copy
  1. @protocol EditViewControllerDelegate <NSObject>  
  2.   
  3. @optional  
  4. - (void)sendAddPerson:(Person *)person;  
  5. - (void)sendEditPerson:(Person *)ePerson;  
  6.   
  7. @end  

[cpp]  view plain copy
  1. @protocol InfoViewControllerDelegate <NSObject>  
  2.   
  3. @optional  
  4. - (void)refreshPersonData:(Person *)personData;  
  5.   
  6. @end  

主视图控制器对代理方法的实现


[cpp]  view plain copy
  1. - (void)sendAddPerson:(Person *)person  
  2. {  
  3.     if (!_personList)  
  4.     {  
  5.         _personList = [NSMutableArray array];  
  6.     }  
  7.       
  8.     [_personList addObject:person];  
  9.     [self.tableView reloadData];  
  10.       
  11.     NSString *path = [self pathForPersonList];  
  12.     [NSKeyedArchiver archiveRootObject:_personList toFile:path];  
  13. }  
  14.   
  15. - (void)refreshPersonData:(Person *)personData  
  16. {  
  17.     [_personList removeObjectAtIndex:_personIndex];  
  18.     [_personList insertObject:personData atIndex:_personIndex];  
  19.     [_resultList removeObjectAtIndex:_resultIndex];  
  20.     [_resultList insertObject:personData atIndex:_resultIndex];  
  21.       
  22.     [self.searchDisplayController.searchResultsTableView reloadData];  
  23.     [self.tableView reloadData];  
  24.       
  25.     NSString *path = [self pathForPersonList];  
  26.     [NSKeyedArchiver archiveRootObject:_personList toFile:path];  
  27. }  

然后在相应地试图控制器中使用delegate在编辑或添加完成是,将数据发送过来就可以了。

还有就是注意由于在首页有两个tableview,则显示的数据和编辑,响应点击方法都要设置不同的数据列表进行管理。

下面是响应点击的方法


[cpp]  view plain copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     Person *person;  
  4.     if (tableView == self.tableView)  
  5.     {  
  6.         person = _personList[indexPath.row];  
  7.         _infoVC.person = person;  
  8.         _personIndex = indexPath.row;  
  9.     }  
  10.     else  
  11.     {  
  12.         [self performSegueWithIdentifier:@"InfoSegue" sender:nil];  
  13.         person = _resultList[indexPath.row];  
  14.         _infoVC.person = person;  
  15.         _personIndex = [_personList indexOfObject:person];  
  16.         _resultIndex = indexPath.row;  
  17.     }  
  18. }  

这里在点击搜索显示控制器上的tableview时,需要去perform显示个人信息的场景,并对其数据信息进行保存,以便修改后对搜索数据列表的信息也进行更新。


功能完成了,不过在实现一些方法时可以根据个人习惯对代码进行重构。把源码贴出来

Demo源码:点击打开链接



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值