HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS 8上的UITableView单元格宽度停留在320pt大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正试图用自定义单元格创建一个简单的UITableView,而不使用故事板.

我在iPhone 6模拟器上遇到一个问题,其中表视图的宽度为375(应该),但是内部的单元格的宽度为320.

320号是无法在项目中找到的,因为我不是很难编码.当我设置单元格的背景颜色时,它会扩展375的全部宽度,但是我需要将图像对齐到右侧,只能对齐320,如下图所示.

我不知道是不是因为我缺少限制或有一个错误.任何帮助不胜感激,谢谢!

代码设置表:

- (TBmessageViewCell *)getmessageCellforTableView:(UITableView *)tableView aTindexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"messageCell";
    TBmessageViewCell *cell = (TBmessageViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[TBmessageViewCell alloc] initWithStyle:UITableViewCellStyleDefault reusEIDentifier:cellIdentifier];
        [cell createSubviews];
    }

     // Set the new message and refresh
    [cell setmessage:self.viewmodel.messages[indexPath.row]];
    [cell populateSubviews];
    cell.BACkgroundColor = [UIColor blueColor];

    NSLog(@"cell Width: %f",cell.contentView.frame.size.width);

    return cell;
}

完成TBmessageViewCell:

@implementation TBmessageViewCell

const cGFloat MARGIN = 10.0f;
const cGFloat AVATAR_SIZE = 40.0f;

-(id)initWithStyle:(UITableViewCellStyle *)style reusEIDentifier:(NSString *)reusEIDentifier
{
    if(self = [super initWithStyle:UITableViewCellStyleDefault reusEIDentifier:reusEIDentifier]){
    }

// Sets BACkground and SELEcted BACkground color
self.BACkgroundColor = [UIColor clearColor];
UIView *SELEctionColor = [[UIView alloc] init];
SELEctionColor.BACkgroundColor = [UIColor clearColor];
self.SELEctedBACkgroundView = SELEctionColor;

return self;
}

- (void)populateSubviews
{
    // Set the message body
    [self.messageBodyLabel setText:self.message.body];
    [self.messageBodyLabel setTextAlignment:NSTextAlignmentright];
    CGRect bodyFrame = CGRectMake(MARGIN,MARGIN,self.frame.size.width - (AVATAR_SIZE + (MARGIN * 3)),self.frame.size.height);
    // Calculates the expected frame size based on the font and dimensions of the label
    // FLT_MAX simply means no consTraint in height
    CGSize maximumLabelSize = CGSizeMake(bodyFrame.size.width,FLT_MAX);
    CGRect textRect = [self.message.body boundingRectWithSize:maximumLabelSize
    options:NSStringDrawingUsesLineFragmentOrigin
    attributes:@{NSFontAttributename:self.messageBodyLabel.font}
    context:nil];
    bodyFrame.size.height = textRect.size.height;

    // Setup the new avatar frame (Right aligned)
    CGRect avatarFrame = CGRectMake(bodyFrame.size.width + (MARGIN * 2),AVATAR_SIZE,AVATAR_SIZE);

    // Align to the LEFT side for current user's messages
    if ([[TBConfig userID] isEqualToString:self.message.user.userID]) {
        // Set avatar to left if it's me
        avatarFrame.origin.x = MARGIN;
        bodyFrame.origin.x = AVATAR_SIZE + (MARGIN * 2);
        [self.messageBodyLabel setTextAlignment:NSTextAlignmentLeft];
    }

    self.avatar.frame = avatarFrame;
    self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2;
    self.messageBodyLabel.frame = bodyFrame;

    // Set the new cell height on the main Cell
    CGFloat cellHeight = MAX(bodyFrame.size.height,self.frame.size.height) + MARGIN;
    self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,cellHeight);

    // Set the new Profile avatar
    if (![self.avatar.profilEID isEqualToString:self.message.user.facebookID]) {
        [self.avatar setProfilEID:nil];
        [self.avatar setProfilEID:self.message.user.facebookID];
    }
}

- (void)createSubviews
{
    self.messageBodyLabel = [[UILabel alloc] init];
    self.messageBodyLabel.textColor = [UIColor whiteColor];
    self.messageBodyLabel.lineBreakmode = NSLineBreakByWordWrapping;
    self.messageBodyLabel.numberOfLines = 0;
    [self addSubview:self.messageBodyLabel];

    // Creates the avatar
    self.avatar = [[FBProfilePictureView alloc] init];
    [self.avatar setPictureCropping:FBProfilePictureCroppingSquare];
    [self addSubview:self.avatar];
}

解决方法

在将单元格的尺寸添加显示屏之前,您将打印单元格的大小(在尺寸调整之前).你想像谁会把这个单元格大小?你觉得ot应该从哪里获取表视图宽度?它甚至不知道哪个表视图将要交给.

添加显示器时,单元格将被给予适当的框架.

编辑:哦,你可能不希望该cellIdentifier是静态的.你可能想要* const.

大佬总结

以上是大佬教程为你收集整理的iOS 8上的UITableView单元格宽度停留在320pt全部内容,希望文章能够帮你解决iOS 8上的UITableView单元格宽度停留在320pt所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。