#背景
今天在使用以前封装的tableHandle时发生了报错, 原因是tableHandle没有支持加载xib, 如下:
在实现中只支持代码:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id item = [self itemAtIndexPath:indexPath];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:self.aCellClassName];
if (!cell) {
cell = [(UITableViewCell *)[NSClassFromString(self.aCellClassName) alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:self.aCellClassName];
};
if (self.configureCellBlock) {
self.configureCellBlock(indexPath, item, cell);
}
return cell;
}
#让工具类支持xib 3种思路:
1.文件工具检查工程是否有xib文件,没有则用代码加载(浪费了大量时间,最后也没有完美解决!_!)
2.在.h设置一个标志,需要时候加载xib,默认代码加载(线上使用的方法)
3.使用try-catch(忽然想到的,推荐) ###最终修改的代码
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id item = [self itemAtIndexPath:indexPath];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:self.aCellClassName];
if (!cell) {
@try {
cell = [[[NSBundle mainBundle] loadNibNamed:self.aCellClassName owner:self options:nil]firstObject];
} @catch (NSException *exception) {
cell = [(UITableViewCell *)[NSClassFromString(self.aCellClassName) alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:self.aCellClassName];
} @finally {
}
};
if (self.configureCellBlock) {
self.configureCellBlock(indexPath, item, cell);
}
return cell;
}
###ps:工具类的gitHub: https://github.com/poos/SXHelper ###iOS捕获异常,常用的异常处理方法 http://blog.youkuaiyun.com/u011417413/article/details/51536672