Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 视图中具有多个单元格标识符的Autolayout UITableViewCell_Ios_Uitableview_Ios7_Autolayout - Fatal编程技术网

Ios 视图中具有多个单元格标识符的Autolayout UITableViewCell

Ios 视图中具有多个单元格标识符的Autolayout UITableViewCell,ios,uitableview,ios7,autolayout,Ios,Uitableview,Ios7,Autolayout,在我尝试使用100%autolayout驱动的表视图时,包括根据中的说明自动计算单元格高度时,我发现在同一个表视图中同时混合两种不同的单元格类型时出现了一些奇怪的行为 查看这些显示仅使用单元格类型1或单元格类型2以及尝试在同一表视图中替换它们时行为的图像。很抱歉没有链接,我还没有足够高的声誉来发布链接 表1: 表2: 两者加在一起: 您可以看到,在前两幅图像中,单元格是如何以完美的高度显示的,这样就不会在任何UILabel元素的上方或下方显示额外的空间。在第三幅图中,我在类型1和类型2的单

在我尝试使用100%autolayout驱动的表视图时,包括根据中的说明自动计算单元格高度时,我发现在同一个表视图中同时混合两种不同的单元格类型时出现了一些奇怪的行为

查看这些显示仅使用单元格类型1或单元格类型2以及尝试在同一表视图中替换它们时行为的图像。很抱歉没有链接,我还没有足够高的声誉来发布链接

表1:

表2:

两者加在一起:

您可以看到,在前两幅图像中,单元格是如何以完美的高度显示的,这样就不会在任何UILabel元素的上方或下方显示额外的空间。在第三幅图中,我在类型1和类型2的单元格之间进行了交替,没有进行任何额外的代码更改。当显示单个单元格标识符时,即使文本比上一个单元格长或短,也可以完美地计算高度。我把它们混在一起时会有奇怪的行为

#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (instancetype)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _lipsumArray = [[NSMutableArray alloc] init];
    for (int i=0; i < 10; i++) {
        [_lipsumArray addObject:[self randomLorem]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _lipsumArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self basicCellAtIndexPath:indexPath];
}

- (TableViewCell *)basicCellAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = ([indexPath row] % 2) ? @"TableCell1" : @"TableCell2";
    TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    [self configureTableCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureTableCell:(TableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.mainText.text = [_lipsumArray objectAtIndex:indexPath.row];
    cell.mainText.font = [UIFont fontWithName:@"Avenir-Medium" size:18.0];

    cell.otherText.text = [self textForOriginalBody:[_lipsumArray objectAtIndex:indexPath.row]];
    cell.otherText.font = [UIFont fontWithName:@"Avenir-Medium" size:14.0];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    static TableViewCell *sizingCell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *identifier = ([indexPath row] % 2) ? @"TableCell1" : @"TableCell2";
        sizingCell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
    });
    [self configureTableCell:sizingCell atIndexPath:indexPath];

    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height;
}

- (NSString*)randomLorem {
    NSString *loremIpsum = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
    NSArray *loremArray = [loremIpsum componentsSeparatedByString:@" "];
    int r = arc4random() % [loremArray count];
    r = MAX(3, r); // no less than 3 words
    NSArray *loremRandom = [loremArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, r)]];
    return [NSString stringWithFormat:@"%@", [loremRandom componentsJoinedByString:@" "]];
}

- (NSString*)textForOriginalBody:(NSString*)originalBody {
    if (originalBody.length > 80) {
        originalBody = [NSString stringWithFormat:@"%@...", [originalBody substringToIndex:80]];
    }
    return originalBody;
}

@end
#导入“TableViewController.h”
#导入“TableViewCell.h”
@接口TableViewController()
@结束
@TableViewController的实现
-(instancetype)initWithStyle:(UITableViewStyle)样式{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
_lipsumArray=[[NSMutableArray alloc]init];
对于(int i=0;i<10;i++){
[[u lipsumArray addObject:[self randomLorem]];
}
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回_lipsumArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
返回[self-basicCellAtIndexPath:indexPath];
}
-(TableViewCell*)basicCellAtIndexPath:(NSIndexPath*)indexPath{
NSString*标识符=([indexPath行]%2)?@“TableCell1”:@“TableCell2”;
TableViewCell*cell=[self.tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
[self-configureTableCell:cell-atIndexPath:indexPath];
返回单元;
}
-(void)configureTableCell:(TableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath{
cell.mainText.text=[\u lipsumArray objectAtIndex:indexath.row];
cell.mainText.font=[UIFont fontWithName:@“Avenir中等”大小:18.0];
cell.otherText.text=[self-textForOriginalBody:[\u lipsumArray objectAtIndex:indexath.row]];
cell.otherText.font=[UIFont fontWithName:@“Avenir中等”大小:14.0];
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
静态TableViewCell*sizingCell=nil;
静态调度一次;
一次发送(一次发送)^{
NSString*标识符=([indexPath行]%2)?@“TableCell1”:@“TableCell2”;
sizingCell=[self.tableView dequeueReusableCellWithIdentifier:identifier];
});
[自配置TableCell:sizingCell-atIndexPath:indexPath];
[sizingCell setNeedsLayout];
[sizingCell LayoutifNeed];
CGSize size=[sizingCell.contentView系统布局尺寸设置尺寸:UILayoutFittingCompressedSize];
返回大小。高度;
}
-(NSString*)随机数{
NSString*loremIpsum=@"同一天的知识是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的,是最优秀的当然,除非出于谨慎,否则不应因疏忽而承担责任,不应因疏忽而将法律视为公民的自由。”;
NSArray*loremArray=[loremIpsum组件由字符串分隔:@”“;
int r=arc4random()%[Loremaray计数];
r=MAX(3,r);//不少于3个单词
NSArray*loremRandom=[loremArray objectsAtIndexes:[NSIndexSet indexetwithindexsinrange:NSMakeRange(0,r)];
返回[NSString stringWithFormat:@“%@”,[loremRandom componentsJoinedByString:@”“];
}
-(NSString*)textForOriginalBody:(NSString*)originalBody{
如果(原始体长度>80){
原始正文=[NSString stringWithFormat:@“%@…”,[originalBody substringToIndex:80]];
}
返回原始体;
}
@结束

是什么导致了这种行为?

我知道这是一个老问题,但最近我遇到了一个类似的问题,即在使用自动布局时,表格单元格中UILabel上的随机高度问题。我不知道发生这种情况的技术原因,但在计算高度时没有满足宽度相关性,这就是使用奇怪的布局

为了解决这个问题,我必须对我的表视图单元格进行子类化,然后重写layoutSubviews方法,并在每个标签上设置preferredMaxLayoutWidth值,因为每个标签都会对其自身的帧宽度造成问题。以下是我的swift代码,您可以将其转换为Obj-C:

override func layoutSubviews() {

    super.layoutSubviews()
    self.contentView.layoutIfNeeded()
    self.acCaptionLabel.preferredMaxLayoutWidth = self.acCaptionLabel.frame.size.width
    self.acDataDetails.preferredMaxLayoutWidth = self.acDataDetails.frame.size.width
    self.acCommentsLabel.preferredMaxLayoutWidth = self.acCommentsLabel.frame.size.width

}
希望这有帮助,让我知道如果我的描述不清楚