使用不同的单元格类型创建分组的UITableview [英] Creating Grouped UITableview with Different Cell Types

查看:102
本文介绍了使用不同的单元格类型创建分组的UITableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个分组的uitableview,其中包含每个部分中的一些部分和可能不同的单元格类型。

I need to create a grouped uitableview that includes some sections and possibly different cell types in each sections.

我正在尝试创建类似旧的foursquare应用程序,用户页面(包括'排行榜','朋友建议','朋友','统计','最受探索的类别'......部分)。

I am trying to create something like old foursquare app, user page (includes 'leaderboard', 'friend suggestions', 'friends', 'stats', 'most explored categories' ... sections).

我相当新到ios编程,所以视图可能不是分组的uitableview。

I am fairly new to ios programming, so that view may not be a grouped uitableview.

我特别坚持的是为节创建不同的单元格,并找出单击的单元格。

What I especially stuck is creating different cells for sections, and finding out which cells are clicked.

我的数据源将是2个不同的NSArray *,它由不同的数据类型组成,这就是我需要不同自定义单元格的原因。

My data source will be 2 different NSArray* that consists of different data types, that's why I need different custom cells.

推荐答案

由于您有两组不同的数据,并且需要在不同的部分中显示,因此必须将数据源方法拆分为两个。

Since you have two different sets of data and you need to display both in different sections, you have to split the data source methods into two.

基本上,选择您想要成为的数据集,然后选择离开。

Basically, choose which dataset you want to be first and off you go.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section)return secondArray.count;
    //Essentially, if statements evaluate TRUE and move forward if the inside is 1 or greater (TRUE == 1)
    return firstArray.count;
    //If the first if statement return hits, then the code will never reach this statement which turns this into a lighter if else statement
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section)
    {
        //do stuff with second array and choose cell type x
    }
    else
    {
        //do stuff with first array and choose cell type y
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the cell with: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(indexPath.section)
    {
        //perform action for second dataset
    }
    else
    {
        //perform action for first dataset
    }
}

对于标题,您可以使用其中任何一个方法只是保持与上面相同类型的样式:

For headers, you can use either of these methods and just keep the same type of styling as above:

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

这篇关于使用不同的单元格类型创建分组的UITableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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