当前位置: 代码迷 >> 综合 >> UITableView 学习笔记1
  详细解决方案

UITableView 学习笔记1

热度:89   发布时间:2023-12-20 23:33:14.0

一个 UITableView 对象必须有一个delegate 和一个data source,从MVC设计模式中可以知道。这个data source 介于 app的 data model和 table view 之间,而这个delegate,就管理的是table view的外观和行为。

UITableViewDataSource 有两个必须的方法。 tableView:numberOfRowsInSection: 这个方法告知table view 在每一个section中有多少行,然后这个tableView:cellForRowAtIndexPath:就提供了在table view中每一行的显示的cell。

而 delegate 采用了UITableViewDelegate,它没有必须要实现的方法。

一个 UIViewController对象管理的是一个在navigation bar 下面的一个view。(ps:self.view.bounds就是这个view 的bounds ,而    [UIScreen mainScreen].bounds 对应的是整个设备的bounds,这个不要搞混了).

当UITableView 将要第一次去显示的时候,这个tableview controller 就会发送 reload data 给tableview

当你的table view 是很多子 view 组成的view 的一部分的时候,而不仅仅只是table view的时候,你应该使用的是UIViewController 的子类而不是 UITableViewController 的子类,因为后者默认的行为是用table view在navigation bar 和 tab bar(如果有的话)填充那一块屏幕。

如果你使用UIViewController 而不是UITableViewController去管理一个TableView,那么你就要去实现上面所说的那些任务。当用户点击了table view 的一行,这个table view就会调用 tableView:didSelecteRowAtIndexPath:或者 tableView:accessoryButtonTappedForRowWithIndexPath:由它们的delegate 实现的方法。(后面的那个方法是在用户点击了一行的detail discloseure 按钮的时候才会触发).

一个segue 代表一个从一个原场景切换到另一个目的场景的一个方法。这种设计模式的后果之一就是你可以使用segue去向目的传送数据,但是你不能使用一个segue 去从目的放发送数据给源。要解决这个办法,你需要去创建一个delegate协议去声明一个在destination view controller 要往传数据的的时候去调用的一个方法

下面显示一个协议去传送数据回它的source view controller的实现。

@class SampleViewController;@protocol SampleViewControllerDelegate <NSObject>- (void)addViewControllerDidCancel:(SampleViewController *)controller;
- (void)addViewControllerDidFinish:(SampleViewController *)controller data:(NSString *)item;@end

- (void)addViewControllerDidCancel:(SampleViewController *)controller
{[self dismissViewControllerAnimated:YES completion:^{}];
}- (void)addViewControllerDidFinish:(SampleViewController *)controller data:(NSString *)item
{if ([item length]) {[self.dataController addData:item];[self.tableView reloadData];}[self dismissViewControllerAnimated:YES completion:nil];
}


虽然 numberOfSectionsInTableView:是一个可选的方法,但是如果你的section的行数超过了一行的话你还是要实现的。如果你的table view是静态(static)的,那么你不用实现任何的datasource 方法,这时table view的配置在编译时就已知了。

  相关解决方案