自定义cell

#import 

@interface sunTableViewCell : UITableViewCell
@property(nonatomic,retain) UILabel *textLab;  // 文字
@property(nonatomic,retain) UIImageView *im;   // 图片
-(void)loadImage; //加载图片
+ (instancetype)cellWithTableView:(UITableView *)tableView; // 创建cell 方法
@end
#import "sunTableViewCell.h"

@implementation sunTableViewCell
@synthesize im,textLab;
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        UIImageView *im1 = [UIImageView new];
        [self.contentView addSubview:im1];
        [im1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(10);
            make.top.equalTo(self.contentView).offset(10);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        im = im1;

        UILabel *lab = [UILabel new];
        lab.font = [UIFont systemFontOfSize:15];
        lab.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:lab];
        lab.text = @"牛宝宝对校长说牛柳看见爆哥的二蛋老大了";
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(im1.mas_right).offset(10);
            make.top.equalTo(self.contentView).offset(10);
             make.right.equalTo(self.contentView).offset(-10);
        }];
        textLab = lab;
        
    }
    return self;
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"sunTableViewCell";
    sunTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[sunTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        //取消选中状态
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return cell;
    
}
-(void)upImage:(NSData *)data{
    UIImage *image=[UIImage imageWithData:data];
    im.image = image;
}
-(NSData *)requestData{
    NSURL *url=[NSURL URLWithString:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1476111235&di=186b3303abeb07bfa99456b13ec99073&src=http://g.hiphotos.baidu.com/image/pic/item/03087bf40ad162d9ec74553b14dfa9ec8a13cd7a.jpg"];
    NSData *data=[NSData dataWithContentsOfURL:url];
    
    return data;
}

-(void)loadImage{
    
    if (im.image ==nil) {
        NSData *data = [self requestData];
        dispatch_queue_t mainQueue= dispatch_get_main_queue();
        dispatch_async(mainQueue, ^{
            [self upImage:data];
        });
        
    }else{
        im.image = nil;
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

你可能感兴趣的:(自定义cell)