我有一个带有自定义 UITableViewCells 的 UITableView,它使用 NSDictionary 来填充它们的标签和图像。我的问题是,我有一个从服务器返回的 NSArray,其中包含每个单元格(NSDictionaries)的信息。所有单元格都是 UITableViewCell 的同一个子类,但我需要每个单元格使用不同的数组。这是一些代码:
//I use this method to create the cell, and build it using the NSDictionary passed to it
+ (CategoryCell *)generateCategoryCellWithInfo:(NSDictionary *)dict;
{
CategoryCell *cell;
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil];
cell = [xib objectAtIndex:0];
if (dict != nil)
{
//build the cell
cell.videoTitle.font = [UIFont fontWithName:@"FuturaStd-CondensedBold" size:17.0f];
cell.videoTitle.text = [dict objectForKey:@"title"];
dispatch_async(dispatch_get_global_queue(0, 0), ^(void){
NSData *fullImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[dict objectForKey:@"fullImage"]]];
if (!fullImageData)
{
return;
}
else
{
dispatch_async(dispatch_get_main_queue(), ^(void){
[cell.contentButton setImage:[UIImage imageWithData:fullImageData] forState:UIControlStateNormal];
});
}
});
cell.numberOfViewsLabel.text = [NSString stringWithFormat:@"%@ views", [dict objectForKey:@"views"]];
}
else
{
//set placeholders
cell.numberOfViewsLabel.text = @"1200 people viewed this";
cell.numberOfLikesLabel.text = @"20 people liked this";
[cell.contentButton setImage:[UIImage imageNamed:@"video-table@2x.png"] forState:UIControlStateNormal];
cell.videoTitle.text = @"A Baby Dances in its Car Seat or Something";
}
return cell;
}
这是创建单元格的地方
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CategoryCellIdentifier = @"categoryCell";
CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:CategoryCellIdentifier];
if (!cell)
{
cell = [CategoryCell generateCategoryCellWithInfo:[itemsArray objectAtIndex:0]];
cell.delegate = self;
}
return cell;
}
现在,我只使用 NSArray itemsArray 中的第一个 NSDictionary,但我想让每个单元格在 itemsArray 中传递一个不同的 NSDictionary。有人可以帮我吗?