带有自定义单元格的UITableView

带有自定义单元格的UITableView,uitableview,nsarray,nsdictionary,Uitableview,Nsarray,Nsdictionary,我有一个自定义UITableViewCells的UITableView,它使用NSDictionary来填充标签和图像。我的问题是,我有一个从服务器返回的NSArray,其中包含每个单元格(NSDictionary)的信息。所有单元格都是UITableViewCell的相同子类,但我需要每个单元格使用不同的数组。下面是一些代码: //I use this method to create the cell, and build it using the NSDictionary passed t

我有一个自定义UITableViewCells的UITableView,它使用NSDictionary来填充标签和图像。我的问题是,我有一个从服务器返回的NSArray,其中包含每个单元格(NSDictionary)的信息。所有单元格都是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。有人能帮我吗?

如果您在表格中使用0个部分:

只需将
表视图中的
[itemsArray objectAtIndex:0]
更改为
[itemsArray objectAtIndex:indexath.row]
:cellForRowAtIndexPath:
方法

如果您的表中有多个
部分
我建议添加一个全局变量
NSInteger indexCountForItemsArray并在每次调用
tableView:CellForRowatineXpath:
时将其递增

在这两种情况下我都会向tableViewController添加一个公共方法,如
-(void)setInfo:(NSDictionary)dict
,并将与类方法
+(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;
}