最近突然发现,其实每个应用程序,里面的界面都是用TableView形成的,只不过,每个自定义的Cell是不一样的,所以,之前只是粗略的看了一下TableView的相关知识,这次把它重点研究了一下,把其中遇到的一些问题mark一下。
注意:如果是在storyboard中用到TableView进行设计,那就可以直接拖入TableView controller,就可以了,如果是在xib中进行设计,则不能直接拖入controller,需要进行下面的一些连接操作,否则会出现一些问题。
1 在xib中拖入TableView
2 建立相关连接:
(1)关联dataSource和delegate到File’s owner
(2)在File’s owner上右键,view关联到view上面
3在ViewController中添加协议
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) NSArray *noteArray;
@end
4返回table中有几个组.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
5返回每个组里有几行
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [noteArray count];
}
6实现tableview添加数据源,返回TableView的行数,返回各行cell实例。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.noteArray objectAtIndex:row];
return cell;
}
这样就形成了简单的一个TableView界面。