内置UITableView的自定义UITableViewCell - 自行调整大小不起作用

时间:2014-10-02 12:59:37

标签: ios objective-c uitableview

我在自我调整细胞的新概念方面遇到了麻烦。它们适用于简单的自定义单元格,但是,我试图在我的一个自定义UITableViewCells中有一个UITableView。我以为我已经正确设置了所有内容,单元格内的UITableView有约束,所有内容以及委托和数据源也都连接在一起。发生的事情是ChecklistTableViewCell中的'numberOfRowsInSection'被调用并返回5,但不返回相应的cellForRowAtIndexPath。因此,应该包含另一个UITableView的单元格仅显示为没有内容的较小单元格。

通过Google进行的“研究”告诉我,可能无法调用cellForRowAtIndexPath,因为单元格的空间太小。因此,我将所有单元格的rowHeight设置为某个常量,并显示单元格内的UITableView - 但我放弃了自我调整功能。

因此,我的问题是,自定义单元格不适用于自定义单元格中更复杂的组件,还是我遗漏了一些基本或重要的内容?

首先,我的UIViewController的代码:

@interface MyViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *elements;

@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 500.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - UITableView methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Element *element = self.elements[indexPath.row];
    if (something) {
        ...
    } else if (something else) {
        ChecklistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
        if (cell == nil) {
            cell = [[ChecklistTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
        }
        cell.checklist = element.checklist;
        return cell;
    } else {
        ...
    }
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.elements count];
}

@end

这是我的代码,里面有一个UITableView:

@interface ChecklistTableViewCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *checklistTableView;
@property (strong, nonatomic) NSArray *checklist;

@end


#import "ChecklistTableViewCell.h"

@implementation ChecklistTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    // Setup table view (self-sizing cells!)
    self.checklistTableView.estimatedRowHeight = 50.0;
    self.checklistTableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - UITableView methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChecklistElementTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"someIdentifier"];
    if (cell == nil) {
        cell = [[ChecklistElementTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"someIdentifier"];
    }
    cell.checklistElementTitleLabel.text = self.checklist[indexPath.row];
    return cell;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.checklist count];
}

@end

UIChecklistElementTableViewCell的代码(.m文件中没有'特殊'代码):

@interface ChecklistElementTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *checklistElementTitleLabel;
@property (strong, nonatomic) IBOutlet M13Checkbox *checkbox;

@end

2 个答案:

答案 0 :(得分:1)

最后,我选择实施

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

我返回包含表视图的单元格的计算高度,对于所有其他单元格,我返回UITableViewAutomaticDimension。

这不是我所希望的,但它确实有效。如果有人有其他解决方案,我仍然非常感兴趣。

答案 1 :(得分:0)

你确定你正确设置了Autolayout对单元格元素的约束吗? 您是否将所有UILabel的行号设置为0?

相关问题