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

本文介绍了UITableView如何刷新特定section或cell的方法,并提供了一个计算UILabel高度的实用函数。此外,还讨论了使用SDWebImage加载图片时可能出现的问题及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题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测试版本,继续奋斗!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IOT_Elon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值