您的位置:首页 > 其它

tableView视图列表1: titleForHeaderInSection

2014-12-02 14:12 567 查看
这几个方法都是Data Source Methods,就是数据来源的方法。换句话说就是在这里面你得确定数据的来源。

主要涉及的有这几个方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView;

上面这个方法就是让tableView知道他有几个section,默认为1,当你的数据源有两个块时,就是return 2;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

上面这个方法是让tableView知道他每一个块有几个cell,传进来的是一个section的值,就是第几个块。这时你根据section的值来返回这个块有的cell的个数.比如section为0,返回电影的个数,为1,就是电视剧的个数。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

上面这个方法是让tableView知道他每一个cell是怎么样的。注意到传进来的indexPath吗?他就是“坐标”,他表明了cell在第几个块第几个cell,得到的方法为indexPath.section;indexPath.row;然后你根据“块序号”、“行序号”来构造不用的cell.

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

这个是设置每一个块的title,传进来的参数为section;

但是如果把数据写死的话,镔哥觉得不大好。一般我们都通过NSArray来存储数据,比如用movieArray存储电影,用dramaArray存储电视剧,- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;这个方法里面你就根据section的值分别返回
[movieArray count],[dramaArray count].这样修改起来也方便。

还有就是TableView Delegate,主要是决定将要选定某个cell及选定某个cell后的操作。与只有一个section的判断不同的是,你还得考虑indexPath.section值来确定你要响应的是哪一个cell的事件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: