创建具有不同单元格类型的分组 UITableview

2024-03-25

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

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

我对 ios 编程相当陌生,因此该视图可能不是分组的 uitableview。

我特别困惑的是为部分创建不同的单元格,并找出单击了哪些单元格。

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


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

基本上,选择您想要的第一个数据集然后就可以了。

- (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
    }
}

对于标题,您可以使用以下任一方法,并保持与上面相同的样式类型:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建具有不同单元格类型的分组 UITableview 的相关文章

随机推荐