UITableView动态添加单元格 [英] UITableView adding cells dynamically

查看:38
本文介绍了UITableView动态添加单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的nib文件中有一个UITableView,并希望向该TableView中的每个单元格动态添加一些内容.有没有办法做到这一点?我有一个要在TableView中显示的文本数组以及一个图片数组.

I have a UITableView in my nib file and would like to dynamically add some content to each of the cells in that TableView. Is there a way to do this? I have an array of text that I would like to display in the TableView as well as an array of Pictures.

推荐答案

您需要实现UITableViewDataSource协议并覆盖

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

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

方法.您将要为tableView:numberOfRowsInSection:方法返回数组的长度.在tableView:cellForRowAtIndexPath:方法中,您将创建UITableViewCell(如果可用,请出队)并添加要保存数据的UIView(即UIImage等).使用indexPath.row作为数组的索引访问数据以填充视图.这一切有意义吗?听起来比实际要复杂一些.

methods. You will want to return the length of your array for the tableView:numberOfRowsInSection: method. In the tableView:cellForRowAtIndexPath: method you will get create the UITableViewCell (dequeue one if available) and add the UIViews you want to hold your data (i.e. a UIImage, etc.). Access the data to populate your view using the indexPath.row as the index to your array(s). Does that all make sense? It sounds a bit more complicated than it is in practice.

此处是UITableViewDataSource的文档协议

这篇关于UITableView动态添加单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆