全面解析iOS应用中自定义UITableViewCell的方法

2020-01-15 13:30:04王振洲

选中Table View Cell,打开Attribute Inspector,将Identifier设置为CustomCellIdentifier:

iOS应用,UITableViewCell

为了充分使用这些标签,还要自己创建一些数据,存在plist文件中,后边会做。

7、打开CustomCell.h,添加属性:

复制代码
@property (copy, nonatomic) UIImage *image;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *dec;
@property (copy, nonatomic) NSString *loc;
8、打开CustomCell.m,向其中添加代码:

 

8.1 在@implementation下面添加代码:

复制代码
@synthesize image;
@synthesize name;
@synthesize dec;
@synthesize loc;
8.2 在@end之前添加代码:
复制代码
- (void)setImage:(UIImage *)img {
    if (![img isEqual:image]) {
        image = [img copy];
        self.imageView.image = image;
    }
}

 

-(void)setName:(NSString *)n {
    if (![n isEqualToString:name]) {
        name = [n copy];
        self.nameLabel.text = name;
    }
}

-(void)setDec:(NSString *)d {
    if (![d isEqualToString:dec]) {
        dec = [d copy];
        self.decLabel.text = dec;
    }
}

-(void)setLoc:(NSString *)l {
    if (![l isEqualToString:loc]) {
        loc = [l copy];
        self.locLabel.text = loc;
    }
}


这相当于重写了各个set函数,从而当执行赋值操作时,会执行我们自己写的函数。

 

好了,现在自己定义的Cell已经可以使用了。

不过在此之前,我们先新建一个plist,用于存储想要显示的数据。建立plist文件的方法前面的文章有提到。我们建好一个friendsInfo.plist,往其中添加数据如下: