您的位置:首页 > 移动开发 > IOS开发

IOS-58-阶段性小细节汇总

2016-03-25 18:45 363 查看
问题1:UITableView刷新某个section或某个cell

//一个section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];


问题2:UILabel计算高度与系统不一致,在做多行展示时,常常需要计算label的高度,但是不建议自己计算,比如:有2行文字,系统计算的值是35,而自己写的计算方法可能是37。

计算方法:

/**
*  计算文本的高度
*
*  @param text         文本
*  @param font         字体
*  @param limitedWidth 限定宽度
*
*  @return 高度
*/
+ (CGFloat)heightForLabelWithText:(NSString *)text font:(UIFont *)font limitedWidth:(CGFloat)limitedWidth {

CGSize size = CGSizeMake(limitedWidth, CGFLOAT_MAX);
NSDictionary *attributes = @{NSFontAttributeName : font};
CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
return ceilf(rect.size.height);
}


问题3:本地取数据(图片URL)的时间会对第三方SDWebImage库产生影响

我们常常会用到这个方法:

[self.headImage sd_setImageWithURL:[NSURL URLWithString:[NSUserDefaults standardUserDefaults]objectForKey:@"HeadImgUrl"];] placeholderImage:[UIImage imageNamed:@"me_woman_head"]];


但是,[NSUserDefaults standardUserDefaults]objectForKey:@”HeadImgUrl”];需要一定的读取时间,这样会造成上面的方法以为没有图片URL地址,就会默认显示后面的图片。

解决方法:

NSString *headUrl = [NSUserDefaults standardUserDefaults];//提前取值是为了避免读取数据时的时间对下面的方法产生影响
[self.headImage sd_setImageWithURL:[NSURL URLWithString:headUrl] placeholderImage:[UIImage imageNamed:@"me_default_head"]]


最后总结:项目已进行接近5个月,前天提交了2.2.0测试版本,继续奋斗!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: