1. cell的插入
cell的插入属于编辑功能,其核心代码类似,比如设置编辑风格,更新数据,同步ui
// UITableViewCellEditingStyleInsert 插入的行为
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
//当点击加号的时候,该代理方法将会调用
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *arr = self.dataArray[indexPath.section];
[arr insertObject:@"New data" atIndex:indexPath.row+1];
// [self.tableView reloadData]; // 全部重新刷新,没有动画,效率低
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
}
2. 自定义cell: 使用代码自定制

3. 自定义cell: 使用xib自定制

数据
NSArray *names = @[@"花菜", @"辣椒", @"白菜", @"碗豆", @"萝卜", @"大蒜", @"土豆", @"竹笋", @"白萝卜", @"大葱", @"南瓜", @"茄子", @"香菇", @"西红柿"];

代理方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"VegetableCellID";
VegetableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// cell = [[VegetableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell = [[[NSBundle mainBundle] loadNibNamed:@"VegetableCell" owner:nil options:nil] lastObject];
}
cell.vegetable = self.vegetables[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
4. 文本高度问题
行高问题:
rowHeight
优先级低于heightForRowAtIndexPath
如何动态计算行高
/**
* NSStringDrawingUsesLineFragmentOrigin:
绘制文本时使用 line fragement origin 而不是 baseline origin
* NSStringDrawingUsesFontLeading:
计算行高时使用行距。(译者注:字体大小+行间距=行距)
* NSStringDrawingTruncatesLastVisibleLine:
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
* NSStringDrawingUsesDeviceMetrics:
计算布局时使用图元字形(而不是印刷字体)。
*/
CGSize size = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil].size;
#import "RootViewController.h"
#import "NSString+height.h"
@interface RootViewController ()
@property (nonatomic, retain) UILabel *targetLabel;
@property (nonatomic, retain) NSMutableArray *textArray;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self fillTextArray];
[self addTargetLabel];
[self addTestButton];
}
- (void)addTestButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, self.view.frame.size.height-80, self.view.frame.size.width, 35);
[button setTitle:@"Test!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClick:(id)sender {
int index = arc4random()%self.textArray.count;
NSString *text = self.textArray[index];
/*
// 计算text的高度,改变label的高,让label适应这些文字
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:18.0] constrainedToSize:CGSizeMake(self.targetLabel.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
// 第一个参数:字体
// 第二个参数:constraintedSize
// 第三个参数:换行模式
*/
// after iOS7: boundingRectWithSize:options:attributes:context:
CGSize textSize = [text boundingRectWithSize:CGSizeMake(self.targetLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18.0]} context:nil].size;
// 选项options:
/**
* NSStringDrawingUsesLineFragmentOrigin:
绘制文本时使用 line fragement origin 而不是 baseline origin
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine
* NSStringDrawingUsesFontLeading:
计算行高时使用行距。(译者注:字体大小+行间距=行距)
* NSStringDrawingTruncatesLastVisibleLine:
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。
* NSStringDrawingUsesDeviceMetrics:
计算布局时使用图元字形(而不是印刷字体)。
*/
CGRect frame = self.targetLabel.frame;
frame.size.height = textSize.height;
self.targetLabel.frame = frame;
self.targetLabel.text = text;
}
- (void)fillTextArray {
self.textArray = [NSMutableArray arrayWithObjects:@"ifdsflsfjsajflsjal;fjda;fjasjfdajf;dasfjajsk我是中国人革柑械地革柑械地革柑械地革柑械地革柑械地 kdlsfjl革柑械地革柑械地革柑械地革柑械地dsjfdlsjflsfjsf\nfdslfjlsfjlsffds革柑械地革柑械地革柑械地fdsfsingagjlafa", @"12345678jhgfdahgfdafggafshgfdagdsfgfdsafgafafgrgaa\n\nagaga我们祖革柑械地革柑械地革柑械地革柑械地革柑械地画", @"ingldsfls革柑械地革柑械地革柑械地djfls画图加", @"infdlsjfldsjflsfjlsajfljsa\nfjlafjsa革柑械地 城厅埃里克森轩历革", @"safljafe78wfjlahfkjaflhaifhafhkafka fds", @"sjflsfjlsajfljsa\nfj革柑械地革柑械地革柑械地lafjsa革", nil];
}
- (void)addTargetLabel {
self.targetLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 40, self.view.frame.size.width-40, 20)] autorelease];
self.targetLabel.backgroundColor = [UIColor yellowColor];
self.targetLabel.numberOfLines = 0;
self.targetLabel.font = [UIFont systemFontOfSize:18.0];
[self.view addSubview:_targetLabel];
}
- (void)dealloc {
[_targetLabel release];
[_textArray release];
[super dealloc];
}
@end
作业:解决复用问题