使用UITableView,可以直接拖动Table View Controller,这样会生成一堆模板代码,往你们填就行了,但是有时候不能直接这样拉,例如已经有一个UIView(可能在Navigation View或者Tabbed View里面)想在上面添加Table View。下面是步骤。
1.先把Table View控件拉到UIView中。
2.做一个connection绑定,把这个Table View绑定到UIOutlet。确认h文件包含了@property,而m文件包含@synthesize。
3.把tableview的dataSource和delegate绑定(connections)到UIViewController上。
4.继承两个protocols
@interface JLDividendsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
5.重写几个方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.divisions count]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DivisionCell"]; JLDivision *division = [self.divisions objectAtIndex:indexPath.row]; cell.textLabel.text = division.divisionNumber; return cell; }