1. 一个xib文件中创建多个cell
在cellForRowAtIndexPath
方法中查找重用方法。因为xib属于资源文件通过NSBundle
来查找,取第几个cell
。
一个xib
中创建多个cell
可以直接粘贴复制xib
中的cell
也可以在控件库中拖拽,记住如果是从控件库中拖拽的cell
记得要修改Custom Class
,如果不修改关联的属性会无效,只会关联第一个cell
如果是从控件库中拖拽的cell
红色框内是空白,Xcode10的控件库在右上角,老版的在右下角。
![](https://img.haomeiwen.com/i1483397/b1a356732ad6fb71.png)
![](https://img.haomeiwen.com/i1483397/6da7004c1b2f8e3e.png)
xib
中的cell
是有序的按照,从0开始,前提是不修改下图的标识,如果修改需要按照你修改的查找cell
![](https://img.haomeiwen.com/i1483397/f9daff05d6530873.png)
注册cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (!cell) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"UITableViewCell" owner:self options:nil]objectAtIndex:0];
}
return cell;
}
或者让创建的cell
继承下面的BaseTableCell
,就能通过下面的方法查找cell
#import <UIKit/UIKit.h>
@interface BaseTableCell : UITableViewCell
@property (nonatomic, assign) NSInteger cellIndex;
- (id)initWithXibName:(NSString*)strName bundle:(NSBundle *)bundle;
- (id)initWithIndex:(NSInteger)index bundle:(NSBundle *)bundle;
- (CGFloat)calculateHeight;
@end
#import "BaseTableCell.h"
@implementation BaseTableCell
- (id)init{
return [self initWithIndex:0];
}
- (id)initWithXibName:(NSString*)strName{
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:strName owner:self options:nil];
self = [nibs objectAtIndex:0];
if (self) {
self.cellIndex = 0;
}
return self;
}
- (id)initWithIndex:(NSInteger)index{
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
self = [nibs objectAtIndex:index];
if (self) {
self.cellIndex = index;
}
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (CGFloat)calculateHeight
{
self.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.bounds), 0.0f);
[self setNeedsLayout];
[self layoutIfNeeded];
CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
@end
使用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger section = indexPath.section;
if (section == 0) {
static NSString * cellIdentifier = @"identifier";
TopUpCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[TopUpCell alloc] initWithIndex:section];
}
return cell;
}
if (section == 1) {
static NSString * cellIdentifier = @"identifier1";
TopUpCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[TopUpCell alloc] initWithIndex:section];
}
return cell;
}
if (section == 2) {
static NSString * cellIdentifier = @"identifier2";
TopUpCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[TopUpCell alloc] initWithIndex:section];
}
return cell;
}
return nil;
}
2. xib属性关联问题。
新拖进来三个cell
上面放好控件添加过约束后,开始拖拽控制为属性,可是每次关联的都是第一个cell
,最后检查发现是因为新的几个cell
是从控件库中拖拽进去的没有和某个类关联,也就是相当于是一个空的cell跟那个都无关,所以无论怎么拖拽都是关联的第一个cell
。
command+control+鼠标
关联属性问题:
- 点击cell查看属性检查器通过属性检查器和cell某个控件关联,然后在拖拽为属性。
-
提前在.h文件中定义好属性,直接和控件关联,如果有多个cell会有选择关联某个cell选项。
关联
如果有问题可以私信我,或者评论,也有一年多没碰过xib也在学习中。
网友评论