具有多个部分的 iPhone UITableView

Posted

技术标签:

【中文标题】具有多个部分的 iPhone UITableView【英文标题】:iPhone UITableView with Multiple Sections 【发布时间】:2010-06-23 15:02:30 【问题描述】:

抱歉,如果这是一个简单的问题,但谷歌搜索无法帮助我。 我计划在表格视图中显示 3 个数据数组,每个数组在表格视图的不同部分中。

谁能给我一个链接到一个很好的教程或可以帮助我的示例代码?

如果这是一个简单的问题和答案,我再次道歉,但我是 Xcode 的新手,正在为此做我的第一个严肃项目。

谢谢!

【问题讨论】:

【参考方案1】:

在您的 TableViewController 实现中:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 3;



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

 return [[self getDataArray:section]count];//implement getDataArray


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


    static NSString *CellIdentifier = @"Cell";

    NSObject *data = [[self getDataArray:indexPath.section]objectAt:indexPath.row];//implement getDataArray

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
         //add Code to populate cell with data
    
    return cell;

其他可能实现的方法:

- (UIView *)tableView: (UITableView *)tableView viewForHeaderInSection: (NSInteger)section
- (CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section

【讨论】:

非常感谢,我没有意识到它这么简单。无论如何,我已经实现了大部分代码,我只需要完成它!再次感谢您花时间回答。 为什么在网上这么难找到?我已经为此寻找了几天。

以上是关于具有多个部分的 iPhone UITableView的主要内容,如果未能解决你的问题,请参考以下文章

iphone:uitextfield,具有相同委托的多个文本字段?

创建具有多个图标的 iPhone 视图(例如 Yelp、Old Facebook Launcher)

iPhone 核心数据:具有不同属性和字母部分的 NSFetchRequest

具有多个 NIB 的 iPhone 应用程序的最佳实践建议

分组UITableView,多个部分在iPhone SDK中创建问题

iPhone 5 UITableView 不显示