UITableView显示数据的基础步骤
- 设置数据源对象
- 数据源对象必须遵守UITableView的数据源协议UITableViewDataSource
- 在数据源对象中必须实现UITableViewDataSource中某些特定的方法,这些方法告诉了数据源应该如何显示数据
- 当UITableView运行起来的时候,会不断的调用数据源对象的这些方法,来获取对应的数据并显示出来
示例
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource>
@end
@implementation ViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil