我有一個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。有人可以幫我嗎?
完美的作品!謝謝您的幫助。 –
很高興,不客氣 – anneblue