【问题标题】:iPhone - Two different custom cells, with different designs, in same UITableViewController - Cells keep initial sizeiPhone - 两个不同的自定义单元格,具有不同的设计,在同一个 UITableViewController - 单元格保持初始大小
【发布时间】:2012-12-24 06:39:47
【问题描述】:

我正在尝试使用两个不同的自定义单元格制作 UITableViewController。我看过很多帖子,尤其是this,它建议使用两个不同的重用标识符。我正在这样做,我似乎仍然得到了第一个自定义单元格的大小。它没有显示第二个单元格的标签等,但大小似乎相同。数据显示不同,但单元格大小相同。

由于我不知道问题出在哪里,我可能会包含不必要的代码,所以我提前道歉。

这是我的 TableViewController 代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    //set equal to the information in the array
    return [jsonDataArray count];
}

- (void) addHeaderAndFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    v.backgroundColor = [UIColor clearColor];
    //[self.tableView setTableHeaderView:v];
    [self.tableView setTableFooterView:v];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SCellIdentifier = @"SCell";
    static NSString *PCellIdentifier = @"PCell";

    NSLog(@"%@",indexPath);
    NSLog(@"JSONDATAARRAY: %i", jsonDataArray.count);
    NSDictionary *jsoninfo = [jsonDataArray objectAtIndex:indexPath.row];
    if (indexPath.row == 0)
    {
        OBPromoCell *cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:PCellIdentifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[OBPromoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PCellIdentifier];
        }

        //get required keys from dictionary and assign to vairables
        NSString *title = [jsoninfo objectForKey:@"title"];
        NSString *subtitle = [jsoninfo objectForKey:@"subtitle"];
        NSURL *imageURL = [NSURL URLWithString:[jsoninfo objectForKey:@"image_URL"]];

        //download the images. This will most lilely need to be made asynchronous.... I don't think it is now.

        NSData *imgData = [NSData dataWithContentsOfURL:imageURL];
        UIImage *img = [[UIImage alloc] initWithData:imgData];

        //fill in text to cells
        cell.CustomCellTopLabel.text = title;
        cell.CustomCellBottomLabel.text = subtitle;
        cell.CellImageView.image = img;

        return cell;
    }

    else
    {
        OBStandardCell *cell = nil;
       cell = [tableView dequeueReusableCellWithIdentifier:SCellIdentifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[OBStandardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SCellIdentifier];
        }   

        //get required keys from dictionary and assign to vairables
        NSString *title = [jsoninfo objectForKey:@"title"];
        NSString *subtitle = [jsoninfo objectForKey:@"subtitle"];
        NSURL *imageURL = [NSURL URLWithString:[jsoninfo objectForKey:@"series_image_URL"]];

        //download the images. This will most lilely need to be made asynchronous.... I don't think it is now.

        NSData *imgData = [NSData dataWithContentsOfURL:imageURL];
        UIImage *img = [[UIImage alloc] initWithData:imgData];

        //fill in text to cells
        cell.CustomCellTopLabel.text = title;
        cell.CustomCellBottomLabel.text = subtitle;
        cell.CellImageView.image = img;

        return cell;
    }
}

这是我的故事板设置方式的图片,以供参考。同样,它们确实有不同的重用标识符:

感谢您的帮助!!!

【问题讨论】:

    标签: iphone uitableview tableview


    【解决方案1】:

    几乎你已经完成了这些事情。问题与数据显示有关。

    需要实现UITableView的“heightForRowAtIndexPath”方法,根据单元格显示设置高度。

    Method:
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    希望它能解决你的问题。

    干杯。

    【讨论】:

    • 谢谢!就是这样,为什么我以前没有看到它是一个谜。
    【解决方案2】:

    UITableView 有一个属性

    @property(nonatomic) CGFloat rowHeight
    

    你可以设置它,但它会为你的表格中的所有行设置高度,

    如果你想要不同的行高,你应该实现这个 UITableViewDelegate 方法

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多