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)