Mik*_*e Z 5 iphone objective-c uitableview ipad ios
我有一个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)
Run Code Online (Sandbox Code Playgroud)
等等.现在我要做的是创建一个tableView,显示所有这些信息,但根据字典键的不同,使用不同类型的单元格.例如,电影使用cellForMovies书籍使用cellForBooks音乐cellForMusic,每个都有自己的布局.
显然,我过于简单化了,但希望你明白我要做的事情.
如果我为每个部分执行部分和不同的单元格类型,我能够完成此操作,但我不想这样做.我希望能够拥有这样的表格,例如:
cellForBooks
cellForMovies
cellForMovies
cellForMusic
cellForBooks
Run Code Online (Sandbox Code Playgroud)
等等......您可以指出我的任何想法或资源?我只关心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];
Run Code Online (Sandbox Code Playgroud)
然后配置单元格我做了这个:
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";
}
Run Code Online (Sandbox Code Playgroud)
每个都在故事板中有一个不同的tableviewcell.我们想知道您是否要使用任何内置单元格功能,就像cell.textLabel.text您需要这样做:
cell.textLabel.text = [object objectForKey:@"name"];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
Run Code Online (Sandbox Code Playgroud)
希望这有助于将来的其他人.
在您的- (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;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢所有帮助过的人,特别是将最后一块放在一起的阁楼.
最终工作的是将数据结构更改为字典数组,然后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];
Run Code Online (Sandbox Code Playgroud)
然后配置单元格我做了这个:
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";
}
Run Code Online (Sandbox Code Playgroud)
每个都在故事板中有一个不同的tableviewcell.我们想知道您是否要使用任何内置单元格功能,就像cell.textLabel.text您需要这样做:
cell.textLabel.text = [object objectForKey:@"name"];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
Run Code Online (Sandbox Code Playgroud)
希望这有助于将来的其他人.
| 归档时间: |
|
| 查看次数: |
9203 次 |
| 最近记录: |