使用xib的UITableViewCell在编辑状态下时,排版发生变化及解决

xib如下,需求,灰色框及里面的数字随前面文本标题的长度自动进行重新调整

相应 CustomClass如下

//
//  FavoriteItemCell.h
//  LearningEnglish
//
//  Created by w on 14-7-14.
//  Copyright (c) 2014年 xxx.com. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "FavoriteItem.h"

@interface FavoriteItemCell :UITableViewCell

@property (weak, nonatomic) IBOutletUILabel *title_1;
@property (weak, nonatomic) IBOutletUILabel *title_2;
@property (weak, nonatomic) IBOutletUILabel *playNum;
@property (weak, nonatomic) IBOutletUIView  *playNumBG;
@property (weak, nonatomic) IBOutletUIImageView *imageBG;

@property(nonatomic,retain)FavoriteItem * model;


@end

//
//  FavoriteItemCell.m
//  LearningEnglish
//
//  Created by w on 14-7-14.
//  Copyright (c) 2014年 xxx.com. All rights reserved.
//

#import "FavoriteItemCell.h"

@implementation FavoriteItemCell

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

- (void)awakeFromNib
{
    // Initialization code
    
  
}
-(void)setModel:(FavoriteItem *)model
{
    _model = model;
    
    [self UpdateUI];//此处其实在初次创建cell时,会调用,但无效果
}

-(void)drawRect:(CGRect)rect
{
    [selfUpdateUI];   //此处对视图内的尺寸大小修改即可解决
    
}
//
- (void)layoutSubviews
{
    [superlayoutSubviews];
    //处理编辑时,将删除按钮置于顶部
    for (UIView *subviewin self.subviews) {
        
        for (UIView *subview2in subview.subviews) {
            
            if ([NSStringFromClass([subview2class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                [subview bringSubviewToFront:subview2];
            }
        }
    }
    
    [self UpdateUI];
}

//
-(void)UpdateUI
{
    self.title_1.text = _model.title_1;
    self.title_2.text = _model.title_2;
    self.playNum.text = [NSString stringWithFormat:@"%d次",_model.playNum];

    UIFont *font = self.title_1.font;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:font forKey:NSFontAttributeName];
   
    CGRect rect = [_model.title_1 boundingRectWithSize:CGSizeMake(300, 50) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    
    self.title_1.frame = CGRectMake(self.title_1.frame.origin.x,self.title_1.frame.origin.y, rect.size.width, self.title_1.frame.size.height);

    CGRect frame = self.playNumBG.frame;
    frame.origin.x = self.title_1.frame.origin.x + rect.size.width + 5;
 
    
    self.playNumBG.frame = frame;
    _imageBG.image = [UIImage imageNamed:(_model.playNum != 0?@"reback_playnum_bg2":@"reback_playnum_bg1")];

    
   
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

XXXController.m
//编辑
-(void)onEdit
{
    [self.tableViewsetEditing:(!self.tableView.editing)animated:YES];
    
    NSString *strTitle = (self.tableView.isEditing?@"完成":@"编辑");
    self.navigationItem.rightBarButtonItem.title = strTitle;

    //此处对当前可见行进行重新排版计算    
    //未调用以下代码的效果如图1,调用后的效果如图2所示
    NSArray *arrayCells =self.tableView.visibleCells;
    for (UITableViewCell *cellin arrayCells)
    {
        [cell setNeedsDisplay];
    }
    
}




      
                    (图1)                                                                                                   (图2)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值