您的位置:首页 > 其它

细节:一个很SB的问题引发的思考

2015-03-19 10:14 381 查看

看到http:*/icon/img.jpg所想到的

看到这个链接的第一眼我想到的就是这不是一张图片吗?然后很sb的把这个链接放到浏览器的地址栏,回车一敲, 看,图片显示出来了…

这就是我昨天下午犯的错误.整了近一个小时.坐公交车回去的路上想起来了.我去这是个链接啊,尼玛,有无sb了…

下面详细记录一下所犯的经历:

一开始的代码附上

self.main_do.text = store.main_do;
self.juli.text = store.juli;
self.busshopname.text = store.busshopname;
self.main_img.image = [UIImage imageNamed:store.main_img];


看到最后一行了嘛,对就是它,很手贱的根Label赋值写成一样的了,这样写的问题就是自定义cell的UIImageView图片没有显示.解决办法很简单,让人有种敲头的冲动.

附上解决代码:

// 给子控件分发数据
NSURL  *iconURL = [NSURL URLWithString:store.main_img];

[self.main_img sd_setImageWithURL:iconURL placeholderImage:[UIImage imageNamed:@"meijiu"]];

self.main_do.text = store.main_do;
self.juli.text = store.juli;
self.busshopname.text = store.busshopname;


总结下cell显示创建&显示

自定义cell为了不让其他人修改,我们要把xib拖出来的属性放在.m文件而不是放在.h 这样防止暴露

自定义cell 最好提供一个类方法,在自己内部创建好cell返回给使用者,各司其职吗

数据传递最好使用模型,好处,你需要的都给你了,你还想要什么?

创建cell

自定义cell的.h文件
#import <UIKit/UIKit.h>

@class WZStore;
@interface WZNearbyCell : UITableViewCell

@property (nonatomic,strong) WZStore *store;

+(instancetype)cellWithTableView:(UITableView *)tableView;
@end


自定义cell的.m文件
#import "WZNearbyCell.h"
#import "WZStore.h"
#import "UIImageView+WebCache.h"

@interface WZNearbyCell ()

/// 店铺照片
@property (weak, nonatomic) IBOutlet UIImageView *main_img;

/// 店名
@property (weak, nonatomic) IBOutlet UILabel *busshopname;
/// 主营
@property (weak, nonatomic) IBOutlet UILabel *main_do;
/// 距离
@property (weak, nonatomic) IBOutlet UILabel *juli;

@end

@implementation WZNearbyCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
// 1.创建cell
static NSString *cellID = @"WZNearbyCell";

// 2,缓存池找或创建
WZNearbyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[NSBundle  mainBundle] loadNibNamed:@"WZNearbyCell" owner:self options:nil] lastObject];
}
// 3.返回
return cell;
}

-(void)setStore:(WZStore *)store
{
_store = store;

// 给子控件分发数据
NSURL  *iconURL = [NSURL URLWithString:store.main_img];

[self.main_img sd_setImageWithURL:iconURL placeholderImage:[UIImage imageNamed:@"meijiu"]];

self.main_do.text = store.main_do;
self.juli.text = store.juli;
self.busshopname.text = store.busshopname;

}

@end


控制器中实现数据源&代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

WZNearbyCell *cell = [WZNearbyCell cellWithTableView:tableView];

WZStore *store = self.stores[indexPath.row];

cell.store = store;

return cell;

}


类似这样就能实现自定义cell的现实了

顺便写一句@class的作用:这哥们就是为了防死锁的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: