在一个 TableView 中提供许多不同的 UITableViewCells



我有一个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支持(故事板)。

使用解决方案编辑:

非常感谢所有帮助过的人,尤其是阿迪克斯,他把最后的作品放在一起。

最终有效的是将数据的结构更改为字典数组,然后为每个条目添加一个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";
}

其中每个在情节提要中都有不同的表视图单元格。我们弄清楚了您是否要使用任何内置单元格功能,例如您需要这样做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;
}

非常感谢所有帮助过的人,尤其是将最后一块放在一起的阿迪克斯。

最终有效的是将数据的结构更改为字典数组,然后为每个条目添加一个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";
}

其中每个在情节提要中都有不同的表视图单元格。我们弄清楚了您是否要使用任何内置单元格功能,例如您需要这样做cell.textLabel.text

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

希望这将在未来对其他人有所帮助。

在情节提要中,将表视图设置为使用动态原型。然后,为要使用的每种单元格创建一个单元格。适当地自定义它们,然后在检查器中为每个标识符(例如 MovieCell)提供唯一的重用标识符。

然后,在 tableview:cellForRowAtIndexPath: 方法中,为该索引路径上的内容确定适当的单元格类型。如果 indexPath 中的单元格应该是电影单元格,则可以使用以下方法创建单元格:

static NSString * MovieCellIdentifier = @"MovieCell"; // This must match the storyboard
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MovieCellIdentifier];

这将为您提供在故事板中指定为"MovieCell"的单元格。如果需要进一步自定义它,请创建一个 UITableViewCell 子类,并为情节提要中的单元格指定该子类。然后,您可以通过转换返回的单元格来参考此处的属性等进行自定义:

MovieTableViewCell *movieCell = (MovieTableViewCell *)cell;
// customize

我的观点是,您应该在一个单元格中实现所有单元格类型。这意味着你应该有一个UITableViewCell类和 .xib 用于所有这些类。然后在cellForRowAtIndexPath中创建单元格,然后根据实际要求显示/隐藏构成书籍或电影的子视图。

在这样的设计中,您的电池会有些重量级。但是,它仍然比使用多个单元格类更好,因为这样单元格重用更有效([UITableView dequeueReusableCellWithIdentifier])。

我相信UITableView是针对一种类型的单元格设计的。因此,如果您的布局不同,您仍然可以将它们放在一个位置,并使用框架或显示/隐藏来控制单元格行为。

我认为您应该使用以下内容作为您的单元格标识符。

- (UITableViewCell *)tableView:(UITableView *)tmpTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier            =   [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row];
UITableViewCell *cell               =   [tmpTableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (nil == cell) {
    cell    =   [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
// do ur stuff here
}
return cell;
}

最新更新