iOS UITableView(十一) tableView的几个小技巧

本文提供了两种方法来解决iOS TableView分割线显示不全的问题,并介绍了如何在定义cell时加入代码以使加载过程更加平滑。

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

一、我们在加载有分割线的cell的时候会发现它的右端会短一点,并没有到屏幕右侧,我这里给大家提供两个方法,

1、用XIB拉出分割线,加载的时候分割线自然不会消失,还有一个是在inspector 里面有个Separator Insetss 标签 默认是 Default,我们选择一下 发现有个Custom  这时候我们惊奇的发现Left  15  ,这时候我们只要把这个 15  改成 0 , 然后保存, 你就会发现tableview 的分割线跟以前一样了。

2、用代码对它进行修改

在ios7中我们可以用这行代码  [myTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];   但是这个在iOS8中好像不起作用了 下面是解决办法

//在此处加入下面代码
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
//代理中这样写
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

二、在定义cell的时候加入下面代码会让你的tableView加载更平滑

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

三、固定cell.imageView.image的大小

cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:[indexPath row]]];
CGSize itemSize = CGSizeMake(60, 50);
UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值