UITableview是"跳跃"滚动时(使用Autolayout构建)

时间:2015-12-08 08:34:06

标签: ios objective-c uitableview autolayout

我的UITableView内部有动态的行数,行数高度因极端数量而异。当我向上和向下滚动时,我总是认识到一点点"跳跃"或者"滞后"。我的所有数据都来自我的服务器。

这是我创建一个自定义单元格的示例:

static NSString *BLOCKPHONE = @"BLOCKPHONE_ID";

- (void)viewDidLoad 
{

[super viewDidLoad];

[self.tableView registerClass:[GTBlockPhoneNumberView class] forCellReuseIdentifier:BLOCKPHONE];


self.blockDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:BLOCKPHONE,[NSNumber numberWithInt:BLOCK_PHONE], nil];

self.tableView.rowHeight = UITableViewAutomaticDimension;
}

我的代表:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Put my Data from my Server into my "Model" GFBlock
GFBlock *block = [self.node.blocks objectAtIndex:indexPath.row];

//Each Cell has its own CellIdentifier
NSString *cellIdentifier = [self.blockDictionary objectForKey:[NSNumber numberWithInteger:block.blockTypeId]];

//Check for reusability
GTBlockCell *cell  = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if(cell == nil)
{
    //If Cell is nil create the Interface for a new BLockCell with the specific Identifier
    cell = [[GTAppController sharedInstance]  createInterfaceForCell:block withCellIdentifier:cellIdentifier];

}

//Fill the Interface of the Cell with Data
[[GTAppController sharedInstance]  fillInterfaceWithData:block forCell:(GTBlockCell *)cell];

[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

return cell;
}

//My row Heights can vary by extreme amounts so i cannot use a static Cell height, so i use `UITableViewAutomaticDimension`
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}

为我的手机创建界面:

- (GTBlockCell *)createInterfaceForCell:(GFBlock *)block withCellIdentifier:(NSString *)identifier{

//Load dynamically the correct Cell with the correct Class Name
NSString* className = [self.interfacesDict objectForKey:NSStringFromBlockType(block.blockTypeId)];

Class c = NSClassFromString(className);

//Initialize new Cell with Identifier
GTBlockCell* instance = [(GTBlockCell *)[c alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

return instance;
}

使用数据填充特定单元格:

- (GTBlockCell *)fillInterfaceWithData:(GFBlock *)block forCell:(GTBlockCell *)cell {
//Fill Interface for Cell (Cell gets Reused)
return [cell initWithBlock:block];
}

至少我的UITableViewCell:

@implementation GTBlockPhoneNumberView

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];

if (self) {

    self.button = [GTButton new];
    self.button.translatesAutoresizingMaskIntoConstraints = NO;
    self.button.layer.borderColor = [[UIColor colorWithRed:148/255.0f green:187/255.0f blue:12/255.0f alpha:1.0f]CGColor];
    self.button.layer.borderWidth = 1.0f;

    self.phoneImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"phone.png"]];
    self.phoneImage.translatesAutoresizingMaskIntoConstraints = NO;

    self.phoneContainerView = [UIView new];
    self.phoneContainerView.backgroundColor = [UIColor colorWithRed:148/255.0f green:187/255.0f blue:12/255.0f alpha:1.0f];
    self.phoneContainerView.translatesAutoresizingMaskIntoConstraints = NO;


    self.headLineLabel = [[GTLabel alloc]init];
    self.headLineLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.headLineLabel.numberOfLines = 2;
    self.headLineLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:16];

    self.phoneLabel = [[GTLabel alloc]init];
    self.phoneLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.phoneLabel.numberOfLines = 1;
    self.phoneLabel.font = [UIFont fontWithName:@"Arial" size:14];

    [self.contentView addSubview:self.button];
    [self.button addSubview:self.phoneContainerView];
    [self.phoneContainerView addSubview:self.phoneImage];
    [self.button addSubview:self.headLineLabel];
    [self.button addSubview:self.phoneLabel];

}
return self;
}


- (id)initWithBlock:(GFBlock *)block {
self = [super initWithBlock:block];

if (self) {


    for (GFBlockPhoneNumberItem* item in block.values)
    {

        if([item.phoneHeadline isKindOfClass:[NSNull class]] || item.phoneHeadline.length == 0)
        {
            self.headLineLabel.text = NSLocalizedString(@"cd_call", nil);

        } else {
            self.headLineLabel.text = item.phoneHeadline;

        }

        if([item.phoneNumber isKindOfClass:[NSNull class]] || item.phoneNumber.length == 0)
        {
            [self.phoneLabel removeFromSuperview];

        } else {

            self.phoneLabel.text = item.phoneNumber;

        }

    }
}

return self;

}


- (void)updateConstraints
{

if (!self.didSetupConstraints)
{


    NSDictionary *viewsDictionary = @{@"button":self.button, @"phoneContainerView":self.phoneContainerView, @"phoneImage":self.phoneImage, @"headLineLabel":self.headLineLabel, @"phoneLabel":self.phoneLabel};

    NSDictionary *metrics = @{@"vSpacing":@10, @"hSpacing":@10};


    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(==70)]"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5@999-[button]-0-|"
                                                                             options:0
                                                                             metrics:metrics
                                                                               views:viewsDictionary]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[button]-hSpacing-|"
                                                                             options:0
                                                                             metrics:metrics
                                                                               views:viewsDictionary]];


    [self.button addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneContainerView
                                                            attribute:NSLayoutAttributeWidth
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.button
                                                            attribute:NSLayoutAttributeHeight
                                                           multiplier:1.0
                                                             constant:0.0]];

    [self.button addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneContainerView
                                                            attribute:NSLayoutAttributeHeight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.button
                                                            attribute:NSLayoutAttributeHeight
                                                           multiplier:1.0
                                                             constant:0.0]];


    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[phoneContainerView]"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];

    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[phoneContainerView]-0-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];



    [self.phoneContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[phoneImage]-10-|"
                                                                                    options:0
                                                                                    metrics:metrics
                                                                                      views:viewsDictionary]];

    [self.phoneContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[phoneImage]-10-|"
                                                                                    options:0
                                                                                    metrics:metrics
                                                                                      views:viewsDictionary]];



    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[headLineLabel]-2-[phoneLabel]"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];


    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[phoneContainerView]-5-[headLineLabel]-5-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];

    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[phoneLabel]-5-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];

    [self.button addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[phoneContainerView]-5-[phoneLabel]-5-|"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:viewsDictionary]];



    self.didSetupConstraints = YES;
}
[super updateConstraints];

}

1 个答案:

答案 0 :(得分:0)

如果您对数据有所了解,可以实施

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

tableview使用此方法在屏幕的右边缘绘制滚动条。如果可能,请为此提供一些数据,如"大单元格 - > 200.f"或"小细胞 - > 50.f&#34 ;.这有帮助,因为tableview可以更好地了解其内容的大小。