iphone - 一个 TableView 中有许多不同的 UITableViewCells

标签 iphone objective-c ios ipad uitableview

我有一个 NSDictionary,我们会这样说:

key:       value:
name       Bookshelf
movies     Array containing: (Movie 1, Movie 2, Movie 3)
books      Array containing: (Book 1, Book 2, Book 3)
music      Array containing: (Music 1, Music 2, Music 3)

等等。现在我要做的是创建一个 tableView 来显示所有这些信息,但根据字典键的不同使用不同类型的单元格。例如,电影使用 cellForMovies 书籍使用 cellForBooks 音乐使用 cellForMusic,每个都有自己的布局。

显然,我过于简单化了,但希望您能理解我想做的事情。

如果我对每个部分进行分区并使用不同的单元格类型,我就能完成此操作,但我不想这样做。我希望能够有一个这样的表,例如:

cellForBooks
cellForMovies
cellForMovies
cellForMusic
cellForBooks

等等...您有什么想法或资源可以指点我吗?我只关心 iOS 5 支持( Storyboard)。

用解决方案编辑:

非常感谢所有提供帮助的人,尤其是将最后一 block 拼凑在一起的 atticus。

最终的工作是将数据结构更改为字典数组,然后将 Key: type Value: book/movie/music 添加到每个条目。数据结构现在看起来像:

Array[0]: Dictionary: [Key: type Value: book], [Key: name Value: Physics];
Array[1]: Dictionary: [Key: type Value: music], [Key: name Value: Rock];

然后配置我这样做的单元格:

NSString *CellIdentifier = @"Cell";
NSDictionary *object = [self.listOfStuff objectAtIndex:indexPath.row];
if ([[object objectForKey:@"type"] isEqualToString:@"book"]){
    CellIdentifier = @"BookCell";
}else if ([[object objectForKey:@"type"] isEqualToString:@"music"]){
    CellIdentifier = @"MusicCell";
}else if ([[object objectForKey:@"type"] isEqualToString:@"movie"]){
    CellIdentifier = @"MovieCell";
}

其中每一个在 Storyboard中都有不同的 tableviewcell。我们发现如果您想使用任何内置的单元格功能,例如 cell.textLabel.text,您需要这样做:

cell.textLabel.text = [object objectForKey:@"name"];
cell.textLabel.backgroundColor = [UIColor clearColor]; 
cell.textLabel.opaque = NO;

希望这对以后的其他人有帮助。

最佳答案

在您的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中,您可以根据 indexPath 返回多种类型的单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 5) { // Your conditions here to choose between Books and Movies
        BookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookCell"];
        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:@"bookCell"];
        }
        return cell;
    } else {
        MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:@"movieCell"];
        }
        return cell;
    }
return nil;
}

关于iphone - 一个 TableView 中有许多不同的 UITableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10116628/

相关文章:

iphone - 对于苹果处理器内部工作原理的细节,公众了解多少?

ios - 在服务器通知后更新应用程序中的数据 - 即使强制退出?

ios - MFMailComposeViewController打开然后关闭

objective-c - 过渡从 View Controller :toViewController error: children view controllers must have a common parent view controller

ios - 将图像设置为多个 UIImageViews

iphone - 在页面上嵌入 iphone 联系人点击链接

ios - UIImageView over UIButton 不会出现在 iPhone 4 中

iOS - 将文件从另一个应用程序导入我的应用程序

ios - 我可以使用 ARKit 2 测量眼睛吗

ios - 实现基于在 Swift 中作为参数传递的类的动态初始化