【问题标题】:UITableView with custom cells带有自定义单元格的 UITableView
【发布时间】:2013-06-18 21:41:11
【问题描述】:

我有一个带有自定义 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。有人可以帮帮我吗?

【问题讨论】:

    标签: uitableview nsarray nsdictionary


    【解决方案1】:

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

    只需在您的tableView:cellForRowAtIndexPath: 方法中将[itemsArray objectAtIndex: 0] 更改为[itemsArray objectAtIndex: indexPath.row]

    如果您的表中有多个section,我建议添加一个全局变量NSInteger indexCountForItemsArray;,并在每次调用tableView:cellForRowAtIndexPath: 时递增。

    在这两种情况下我会在 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;
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2016-07-13
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多