cell里面 self 和self.contentview的区别

本文介绍了在UITableView中管理UITableViewCell子视图的两种方法,并详细解释了它们之间的区别及应用场景。第一种方法直接向cell添加子视图,适用于不需要随cell内容变化而调整的情况;第二种方法则将子视图添加到cell.contentView上,这种方式下子视图会随cell内容的变化而自动调整位置。

一般我们向cell中添加子视图,有两种方式

[cpp]  view plain  copy
 print ?
  1. 1、[cell addSubview:]  
[cpp]  view plain  copy
 print ?
  1. 2、[cell.contentView addSubview:]  

    区别在于进行cell编辑时,比如cell内容向左移或者右移时,第一种方式子视图不会移动,第二可以,所以这种情况一般使用第二种方式。

    还有在设置backgroundColor时,使用cell设置时左移或者右移颜色是不会变的,而用cell.contentCell设置时,移动后的空白会显示cell的默认颜色,这种情况视实际情况选择。

    其实这两种方式在大多数情况是一样,不用纠结。


本文转载自:http://blog.youkuaiyun.com/cos_sin_tan/article/details/50832753

这是cell里面的信息,请问有排序吗? // // SDNStaticsApplicationsCategoriesCell.m // Omada // // Created by aobinfeng on 2024/5/7. // Copyright © 2024 TP-Link. All rights reserved. // #import "SDNStaticsApplicationsCategoriesCell.h" #import "TPConvTools.h" #import "SDNCommonAPPImageLoadTool.h" #import "TPClientsAvatarManager.h" #import "UIImageView+WebCache.h" NSString *const kSDNStaticsApplicationsCategoriesCellIdentifier = @"kSDNStaticsApplicationsCategoriesCellIdentifier"; @interface SDNStaticsApplicationsCategoriesCell () @property (nonatomic, strong) UIImageView *iconImageView; @property (nonatomic, strong) TPBLabelView *downloadLabelView; @property (nonatomic, strong) TPBLabelView *uploadLabelView; //@property (nonatomic, strong) UIImageView *downloadImageView; //@property (nonatomic, strong) UIImageView *uploadImageView; @property (nonatomic, strong) UIImageView *arrowImageView; @property (nonatomic, strong) UILabel *appNameLabel; @property (nonatomic, strong) UILabel *trafficLabel; //@property (nonatomic, strong) UILabel *downloadLabel; //@property (nonatomic, strong) UILabel *uploadLabel; @property (nonatomic, strong) UILabel *blockLabel; //@property (nonatomic, strong) UIView *lineView; @property (nonatomic, strong) UITapGestureRecognizer *tap; @property (nonatomic, strong) SDNDashboardApplicationTraffic *appTraffic; @property (nonatomic, strong) SDNClientTrafficInfo *userClientTraffic; @end @implementation SDNStaticsApplicationsCategoriesCellModel - (instancetype)initCutomCellModel { if (self = [super initWithCustomCellClass:[SDNStaticsApplicationsCategoriesCell class]]) { self.cellConfigCallback = ^(UITableView *tableView, NSIndexPath * _Nonnull indexPath, UITableViewCell * _Nonnull cell, TPBCustomTableCellBaseModel * _Nonnull tpbCellModel) { SDNStaticsApplicationsCategoriesCellModel *cellModel = (SDNStaticsApplicationsCategoriesCellModel *)tpbCellModel; SDNStaticsApplicationsCategoriesCell *fixedCell = (SDNStaticsApplicationsCategoriesCell *)cell; if (cellModel.appTrafficInfo != nil) { [fixedCell updateWithApplicationTraffic:cellModel.appTrafficInfo]; } if (cellModel.userClientTrafficInfo != nil) { [fixedCell updateWithUserClientTraffic:cellModel.userClientTrafficInfo]; } if (cellModel.blockAppTrafficInfo != nil) { [fixedCell updateWithApplicationTrafficBlock:cellModel.blockAppTrafficInfo]; } if (cellModel.specificAppClientInfo != nil) { [fixedCell updateWithSpecificAppClientInfo:cellModel.specificAppClientInfo]; } if (cellModel.dpiAppClientTraffic != nil) { [fixedCell updateDpiApplicationClientTraffic:cellModel.dpiAppClientTraffic]; } fixedCell.showTotalTrafficLabel = cellModel.showTotalTrafficLabel; fixedCell.isShowArrowImg = cellModel.isShowArrowImg; }; } return self; } @end @implementation SDNStaticsApplicationsCategoriesCell //- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier //{ // self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; // if (self) // { // self.selectionStyle = UITableViewCellSelectionStyleNone; //// [self setup]; // } // return self; //} //- (void)setup //{ // [self addView]; // [self makeConstraints]; //} - (void)setupSubviews { [super setupSubviews]; self.selectionStyle = UITableViewCellSelectionStyleNone; [self.contentView addSubview:self.iconImageView]; [self.contentView addSubview:self.downloadLabelView]; [self.contentView addSubview:self.uploadLabelView]; [self.contentView addSubview:self.arrowImageView]; [self.contentView addSubview:self.appNameLabel]; [self.contentView addSubview:self.blockLabel]; [self.contentView addSubview:self.trafficLabel]; [self updateBottomSeparatorLeadingTo:self.appNameLabel]; } - (void)makeConstraint { [super makeConstraint]; [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.top.equalTo(self.contentView).offset(TPBDesign.list.sectionHeaderHeight); make.bottom.equalTo(self.contentView).offset(-TPBDesign.list.sectionHeaderHeight); make.size.equalTo(@(40)); }]; [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.trailing.equalTo(self.contentView).offset(-8); make.centerY.equalTo(self.iconImageView); make.width.height.equalTo(@24); }]; [self.appNameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.iconImageView); make.leading.equalTo(self.iconImageView.mas_trailing).offset(TPBDesign.list.contentLeading); }]; [self.trafficLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.appNameLabel.mas_trailing).offset(4); make.trailing.lessThanOrEqualTo(self.arrowImageView.mas_leading).offset(-1); make.centerY.equalTo(self.appNameLabel); }]; [self.downloadLabelView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.appNameLabel); make.top.equalTo(self.appNameLabel.mas_bottom).offset(TPBDesign.list.labelSpace); }]; [self.uploadLabelView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.downloadLabelView.mas_trailing).offset(TPBDesign.list.multiLineExtraSpace); make.centerY.equalTo(self.downloadLabelView); make.trailing.lessThanOrEqualTo(self.contentView).offset(-TPBDesign.list.contentLeading); }]; } - (void)updateWithApplicationTraffic:(SDNDashboardApplicationTraffic *)appTraffic { [self updateConstrainsBlock:NO]; self.appNameLabel.text = appTraffic.applicationName; self.trafficLabel.text = [NSString stringWithFormat:@"(%@)", [TPConvTools convertTrafficToStringWithNum:(appTraffic.up + appTraffic.down) andUnitLevel:TPTrafficUnitLevelB andDecimalDigit:2]]; self.uploadLabelView.text = [TPConvTools convBytesToString:appTraffic.up andDecimalDigit:2]; self.downloadLabelView.text = [TPConvTools convBytesToString:appTraffic.down andDecimalDigit:2]; [self.iconImageView sd_setImageWithURL:[SDNCommonAPPImageLoadTool getImageUrlFromAPPId:@(appTraffic.applicationId )] placeholderImage:[SDNCommonAPPImageLoadTool defaultAPPUIImage]]; } - (void)updateWithUserClientTraffic:(SDNClientTrafficInfo *)userClientTraffic { [self updateConstrainsBlock:NO]; self.userClientTraffic = userClientTraffic; self.appNameLabel.text = userClientTraffic.clientName; self.trafficLabel.text = [NSString stringWithFormat:@"(%@)", [TPConvTools convertTrafficToStringWithNum:(userClientTraffic.up + userClientTraffic.down) andUnitLevel:TPTrafficUnitLevelB andDecimalDigit:2]]; self.uploadLabelView.text = [TPConvTools convBytesToString:userClientTraffic.up andDecimalDigit:2]; self.downloadLabelView.text = [TPConvTools convBytesToString:userClientTraffic.down andDecimalDigit:2]; self.iconImageView.image = [TPClientsAvatarManager imageOfAvatarType:userClientTraffic.type size:TPClientAvatarSize48 isWireless:YES]; } - (void)updateWithApplicationTrafficBlock:(SDNDashboardApplicationTraffic *)appTraffic { [self updateConstrainsBlock:YES]; self.appNameLabel.text = appTraffic.applicationName; self.trafficLabel.text = @""; self.blockLabel.text = [NSString stringWithFormat:@"%@:%ld",gMeshQuickSetup.sdnStaticBlockTimes ,appTraffic.block]; [self.iconImageView sd_setImageWithURL:[SDNCommonAPPImageLoadTool getImageUrlFromAPPId:@(appTraffic.applicationId) ] placeholderImage:[SDNCommonAPPImageLoadTool defaultAPPUIImage]]; } - (void)updateWithSpecificAppClientInfo:(SDNSpecificAppClientInfo *)appTraffic { [self updateConstrainsBlock:NO]; self.appNameLabel.text = appTraffic.clientName; self.trafficLabel.text = [NSString stringWithFormat:@"(%@)", [TPConvTools convertTrafficToStringWithNum:appTraffic.traffic andUnitLevel:TPTrafficUnitLevelB andDecimalDigit:2]]; self.uploadLabelView.text = [TPConvTools convBytesToString:appTraffic.upload andDecimalDigit:2]; self.downloadLabelView.text = [TPConvTools convBytesToString:appTraffic.download andDecimalDigit:2]; self.iconImageView.image = [TPClientsAvatarManager imageOfAvatarType:appTraffic.type size:TPClientAvatarSize48 isWireless:YES]; } - (void)updateDpiApplicationClientTraffic:(SDNCategoryApplicationClientTraffic *)clientTraffic { [self updateConstrainsBlock:NO]; self.appNameLabel.text = clientTraffic.clientName; self.trafficLabel.text = [NSString stringWithFormat:@"(%@)", [TPConvTools convertTrafficToStringWithNum:(clientTraffic.up + clientTraffic.down) andUnitLevel:TPTrafficUnitLevelB andDecimalDigit:2]]; self.uploadLabelView.text = [TPConvTools convBytesToString:clientTraffic.up andDecimalDigit:2]; self.downloadLabelView.text = [TPConvTools convBytesToString:clientTraffic.down andDecimalDigit:2]; self.iconImageView.image = [TPClientsAvatarManager imageOfAvatarType:clientTraffic.type size:TPClientAvatarSize48 isWireless:YES]; } - (void)updateConstrainsBlock:(BOOL)isBlock { [self.blockLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.appNameLabel); make.top.equalTo(self.appNameLabel.mas_bottom).offset(4); }]; if (isBlock) { self.blockLabel.hidden = NO; self.uploadLabelView.hidden = YES; self.downloadLabelView.hidden = YES; } else { self.blockLabel.hidden = YES; self.uploadLabelView.hidden = NO; self.downloadLabelView.hidden = NO; } } - (UILabel *)appNameLabel { if (!_appNameLabel) { _appNameLabel = [[UILabel alloc] init]; _appNameLabel.font = [UIFont tpbListTitle]; _appNameLabel.textColor = [UIColor tpbTextPrimary]; _appNameLabel.numberOfLines = 1; _appNameLabel.lineBreakMode = NSLineBreakByTruncatingTail; } return _appNameLabel; } - (UILabel *)trafficLabel { if (!_trafficLabel) { _trafficLabel = [[UILabel alloc] init]; _trafficLabel.textColor = UIColor.tpbTextThirdContent; _trafficLabel.font = [UIFont tpbListSubtitle]; _trafficLabel.textAlignment = NSTextAlignmentLeft; [_trafficLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; } return _trafficLabel; } - (UILabel *)blockLabel { if (!_blockLabel) { _blockLabel = [[UILabel alloc] init]; _blockLabel.font = [UIFont tpbListSubtitle]; _blockLabel.textColor = [UIColor tpbTextThirdContent]; _blockLabel.numberOfLines = 1; _blockLabel.lineBreakMode = NSLineBreakByTruncatingTail; } return _blockLabel; } - (UIImageView *)iconImageView { if (!_iconImageView) { _iconImageView = [UIImageView new]; // _iconImageView.image = [UIImage imageNamed:@""]; _iconImageView.contentMode = UIViewContentModeScaleAspectFit; _iconImageView.layer.cornerRadius = 8; _iconImageView.layer.masksToBounds = YES; } return _iconImageView; } - (TPBLabelView *)downloadLabelView { if (!_downloadLabelView) { _downloadLabelView = [TPBLabelView labelWithImage: [UIImage imageNamed:@"SDNGatewayPortDownload"] text:nil textColor:[UIColor tpbTextThirdContent]]; _downloadLabelView.textFont = [UIFont tpbListSubtitle]; } return _downloadLabelView; } - (TPBLabelView *)uploadLabelView { if (!_uploadLabelView) { _uploadLabelView = [TPBLabelView labelWithImage: [UIImage imageNamed:@"SDNGatewayPortUpload"] text:nil textColor:[UIColor tpbTextThirdContent]]; _uploadLabelView.textFont = [UIFont tpbListSubtitle]; } return _uploadLabelView; } //- (UIImageView *)downloadImageView //{ // if (!_downloadImageView) // { // _downloadImageView = [UIImageView new]; // _downloadImageView.image = [UIImage imageNamed:@"SDNGatewayPortDownload"]; // _downloadImageView.contentMode = UIViewContentModeScaleAspectFit; // } // return _downloadImageView; //} // //- (UIImageView *)uploadImageView //{ // if (!_uploadImageView) // { // _uploadImageView = [UIImageView new]; // _uploadImageView.image = [UIImage imageNamed:@"SDNGatewayPortUpload"]; // _uploadImageView.contentMode = UIViewContentModeScaleAspectFit; // [_uploadImageView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; // } // return _uploadImageView; //} - (UIImageView *)arrowImageView { if (!_arrowImageView) { _arrowImageView = [UIImageView new]; _arrowImageView.image = [[UIImage tpbImageNamed:@"cellRightIndicator"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; if (TPISRTL) { _arrowImageView.image = [_arrowImageView.image imageWithHorizontallyFlippedOrientation]; } _arrowImageView.tintColor = [UIColor tpbTextThirdContent]; _arrowImageView.contentMode = UIViewContentModeScaleAspectFit; } return _arrowImageView; } //- (UIView *)lineView //{ // if (!_lineView) // { // _lineView = [UIView new]; // _lineView.backgroundColor = [UIColor tpbTextPrimary]; // } // return _lineView; //} - (void)setIsShowArrowImg:(BOOL)isShowArrowImg { _isShowArrowImg = isShowArrowImg; self.arrowImageView.hidden = !isShowArrowImg; } - (void)setShowTotalTrafficLabel:(BOOL)showTotalTrafficLabel { _showTotalTrafficLabel = showTotalTrafficLabel; self.trafficLabel.hidden = !showTotalTrafficLabel; } #pragma mark - ### ADA ### - (UIAccessibilityTraits)accessibilityTraits { return UIAccessibilityTraitStaticText; } - (NSString *)tpbAccessibilityLabel { NSMutableArray *array = [NSMutableArray new]; if (!TPBIsEmptyString(self.appNameLabel.text)) { [array addObject:gAccessibility.clientName]; [array addObject:self.appNameLabel.text]; } if (!TPBIsEmptyString(self.downloadLabelView.text)) { [array addObject:gAccessibility.downloadTraffic]; [array addObject:self.downloadLabelView.text]; } if (!TPBIsEmptyString(self.uploadLabelView.text)) { [array addObject:gAccessibility.uploadTraffic]; [array addObject:self.uploadLabelView.text]; } return [TPBA11yHelper combineAccessibilityTextArray:array]; } @end
11-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值