【问题标题】:UITableView Lags Due a Particular ObjectUITableView 由于特定对象而滞后
【发布时间】:2014-03-10 11:32:13
【问题描述】:

在我的应用程序的主页中,我有一个滚动图像的 iCarousel 对象。在这下面有一个UITableView - 表格视图是问题所在。

最近我在表格视图中为每个cell 添加了一个SevenSwitch 对象,从那时起滚动滞后很多!这是我在tableView:cellForRowAtIndexPath:方法中添加的代码:

cella.subscribed = [[SevenSwitch alloc] initWithFrame:CGRectMake(cella.frame.size.width-60, cella.frame.size.height / 2 - 12, 50, 25)];
cella.subscribed.offImage = [UIImage imageNamed:@"off.png"];
cella.subscribed.onImage = [UIImage imageNamed:@"on.png"];
cella.subscribed.thumbTintColor = [UIColor colorWithRed:(230/255.0) green:(230/255.0) blue:(230/255.0) alpha:1];
cella.subscribed.activeColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
cella.subscribed.inactiveColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
cella.subscribed.onTintColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
cella.subscribed.isRounded = NO;
cella.subscribed.tag = [[tempCat objectForKey:@"Id"] intValue];

[cella.subscribed addTarget:self action:@selector(changeSingleCategory:) forControlEvents:UIControlEventValueChanged];

if ([[tempCat objectForKey:@"Subscribed"] isEqualToString:@"Y"]) {
    cella.subscribed.on = YES;
} else {
    cella.subscribed.on = NO;
}

[cella.contentView addSubview:cella.subscribed];

有没有一种方法可以创建自定义对象而无需每次都设置所有属性?

【问题讨论】:

  • 您是否进行了分析以找出导致延迟的原因?
  • 我很确定 SevenSwitch 会导致滞后,因为如果我从任何单元格中删除它,滞后就会消失......
  • 您需要进行分析才能准确了解它是什么。你总是重新创建开关吗?只有在创建单元格时才应创建开关...
  • 是的,我总是重新创建开关...
  • 我第一次向下滚动表格视图时,它没有任何延迟。当我反复上下滚动我的表格视图时,延迟就开始了。

标签: uitableview lag reusability uiswitch


【解决方案1】:

表格视图中单元格的全部意义在于它们可以重复使用。这不仅适用于单元格,还适用于内容(子视图)。

您必须已经有一个单元子类,因此您应该将开关创建和配置代码放入该类(并将其添加为子视图)。现在,每次你创建/出列一个单元格时,你已经有了一个开关(希望这在 99% 的时间里被重复使用),你需要做的就是使用适当的设置调用 cella.subscribed.on = ...

【讨论】:

    【解决方案2】:

    我有di UITableViewCell.h

    #import "SevenSwitch.h"
    
    @interface cellaMain : UITableViewCell {
    
        SevenSwitch *subscribed;
    
    }
    
    @property (nonatomic, retain) IBOutlet UIImageView *imageMain;
    @property (nonatomic, retain) IBOutlet UILabel *titleMain;
    @property (nonatomic,strong) SevenSwitch *subscribed;
    

    并将这些方法添加到 mi .m 文件中

        -(id) initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            subscribed = [[SevenSwitch alloc] initWithFrame:CGRectMake(frame.size.width-60, frame.size.height / 2 - 12, 50, 25)];
            subscribed.offImage = [UIImage imageNamed:@"off.png"];
            subscribed.onImage = [UIImage imageNamed:@"on.png"];
            subscribed.thumbTintColor = [UIColor colorWithRed:(230/255.0) green:(230/255.0) blue:(230/255.0) alpha:1];
            subscribed.activeColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
            subscribed.inactiveColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
            subscribed.onTintColor = [UIColor colorWithRed:(204/255.0) green:(204/255.0) blue:(204/255.0) alpha:1];
            subscribed.isRounded = NO;
            [self.contentView addSubview:subscribed];
    
        }
    
        return self;
    
    }
    
    -(void)layoutSubviews {
    
        [super layoutSubviews];
    
    }
    

    在方法中

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

    我删除了之前的所有代码,只留下了这个

    cella.subscribed.tag = [[tempCat objectForKey:@"Id"] intValue];
    
    [cella.subscribed addTarget:self action:@selector(changeSingleCategory:) forControlEvents:UIControlEventValueChanged];
    
    if ([[tempCat objectForKey:@"Subscribed"] isEqualToString:@"Y"]) {
        cella.subscribed.on = YES;
    } else {
        cella.subscribed.on = NO;
    }
    

    现在的问题是没有出现切换按钮。

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多