#UITableView 继承 UIScrollView
##内容
###有两个协议
1. 内容
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;
2. 操作
@property (nonatomic, assign) id <UITableViewDelegate> delegate;
###区数 行数
- (NSInteger)numberOfSections;
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
头 尾 2选1 or 0- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
###增删读移动
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
UITableViewDataSource 协议方法
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 建立
@optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 区
- (NSString *)tableView:(UITableView *)tableView titleForHeader(/Footer)InSection:(NSInteger)section;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; // 可编辑
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // 可移动
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // 目录索引
// 编辑按钮的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.arr removeObjectAtIndex:indexPath.row];
// [tableView reloadData];
// 通过tableView来删除上面的cell
// 第一个参数:制定删除哪个分区的哪行作为他的第一个元素放在数组中
// 删除动画
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *str = [self.arr[sourceIndexPath.row] retain];
[self.arr removeObjectAtIndex:sourceIndexPath.row];
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];
}
********************************************************************************************
#pragma iOS8.0之后出现的方法,可以编辑多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deletAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.arr removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}];
UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
if (indexPath.row) {
NSString *name = self.arr[indexPath.row];
[self.arr removeObjectAtIndex:indexPath.row];
[self.arr insertObject:name atIndex:0];
NSIndexPath *indexPathZero = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:indexPathZero];
}
}];
return @[deletAction, topAction];
}
// 三种样式;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
#pragma 重写系统的编辑按钮点击方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
UITableView *tabelView = (UITableView *)[self.view viewWithTag:1000];
[super setEditing:editing animated:YES];
[tabelView setEditing:editing animated:YES];
}
##自定义tableViewCell
#pragma 重写cell的初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 完成的属性视图的创建,但是一般创建之后不给属性视图fram
[self createView];
}
return self;
}
- (void)createView{
self.leftImageView = [[UIImageView alloc] init];
self.rightImageView = [[UIImageView alloc] init];
self.topLabel = [[UILabel alloc] init];
self.bottomlabel = [[UILabel alloc] init];
self.bottomlabel.backgroundColor = [UIColor yellowColor];
self.topLabel.backgroundColor = [UIColor redColor];
self.rightImageView.backgroundColor = [UIColor blueColor];
self.leftImageView.backgroundColor = [UIColor cyanColor];
// 添加
[self.contentView addSubview:self.leftImageView];
[self.contentView addSubview:self.rightImageView];
[self.contentView addSubview:self.topLabel];
[self.contentView addSubview:self.bottomlabel];
[_rightImageView release];
[_leftImageView release];
[_topLabel release];
[_bottomlabel release];
}
#pragma mark 这个方法是cell显示之前走得最后一个方法,一般会在方法里设置所有属性视图的大小和尺寸,这个方法会用在图片文字自适应的位置上
- (void)layoutSubviews{
// 重写了父类的方法, 别忘了[supper layoutSubviews];
[super layoutSubviews];
self.leftImageView.frame = CGRectMake(0, 0, WIDTH / 3, HEIGHT);
self.topLabel.frame = CGRectMake(WIDTH / 3, 0, WIDTH / 3, HEIGHT / 2);
self.bottomlabel.frame = CGRectMake(WIDTH / 3, HEIGHT / 2, WIDTH / 3, HEIGHT / 2);
self.rightImageView.frame = CGRectMake(WIDTH / 3 * 2, 0 , WIDTH / 3, HEIGHT);
}
- (void)dealloc{
[_rightImageView release];
[_leftImageView release];
[_bottomlabel release];
[_topLabel release];
[super dealloc];
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
##cell 自适应高度
###tableView
#pragma mark 这个方法是tableview的delegate所提供的协议方法,主要是设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 根据图片的尺寸设置行高
UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
// 计算labl的高度
// 根据对应的文字求出cell上label显示的高度
NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
// 根据文字的大小,计算出文本的尺寸 执行一个尺寸(375, 0);
CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
return image.size.height / image.size.width * self.view.frame.size.width + rect.size.height;
}
###自定义cell- (void)layoutSubviews{
CGSize picSize = self.myImageView.image.size;
self.myImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width , self.contentView.frame.size.width * picSize.height / picSize.width);
// 默认字体17号
NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
// 根据文字的大小,计算出文本的尺寸 执行一个尺寸(375, 0);
CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
self.myLabel.frame = rect;
self.myLabel.center = CGPointMake(self.myLabel.center.x, self.myLabel.center.y + self.myImageView.frame.size.height);
[super layoutSubviews];
}