UITableView与自定义单元格
问题描述:
我有一个UITableView与自定义UITableViewCells,它使用NSDictionary来填充其标签和图像。我的问题是,我有一个从服务器返回的NSArray,其中包含每个单元格(NSDictionaries)的信息。所有单元格都是UITableViewCell的相同子类,但我需要每个单元格使用不同的数组。下面是一些代码:UITableView与自定义单元格
//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:@"[email protected]"] 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;
}
现在,我只是用第一的NSDictionary的NSArray的itemsArray,但我想有每个单元传递itemsArray中的一个不同的NSDictionary。有人可以帮我吗?
答
如果你用你的表0部分:
只要改变[itemsArray objectAtIndex: 0]
到[itemsArray objectAtIndex: indexPath.row]
在你tableView:cellForRowAtIndexPath:
方法。
如果你有你的表我会建议增加一个全局变量NSInteger indexCountForItemsArray;
每次tableView:cellForRowAtIndexPath:
增加它不止一个section
被调用。
在这两种情况下我想补充的公共方法像-(void)setInfo:(NSDictionary)dict
到tableViewController,并比相同/相似的代码就像在你的类方法+(CategoryCell*)generateCategoryCellWithInfo:(NSDictionary *)dict
:
- (void) setInfo : (NSDictionary*) dict {
if (dict != nil) {
//build the cell (your known code here)
}
}
- (UITableViewCell*) tableView : (UITableView*) tableView
cellForRowAtIndexPath : (NSIndexPath*) indexPath
{
static NSString *CategoryCellIdentifier = @"categoryCell";
CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:CategoryCellIdentifier];
if (cell == nil) {
cell = [CategoryCell generateCategoryCellWithInfo : [itemsArray objectAtIndex: indexCountForItemsArray]];
cell.delegate = self;
} else {
[cell setInfo : [itemsArray objectAtIndex: indexCountForItemsArray]];
}
self.indexCountForItemsArray += 1;
return cell;
}
希望这可以帮助你。
完美的作品!谢谢您的帮助。 –
很高兴,不客气 – anneblue