1

我的应用程序中的大多数视图都是 UIViewController 中的 UITableVlews。尝试滚动表格时,我的应用程序感觉像是滞后了。我想知道 (1.) 在表格视图中创建单元格对象,还是在运行时创建它们并将它们添加到单元格子视图中更好?

例子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            case 3: 
            {
                NSNumber *tetherState = [[mobIntDict objectForKey:@"State"] intValue];
                NSNumber *currValState = [[NSNumber numberWithInt:1023] intValue];
                tetherSw = [[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)];
                tetherSw.tag = kDefaultSwTag;
                if(tetherState == currValState){
                    tetherSw.on = YES;
                }else{
                    tetherSw.on = NO;
                }
                [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged];
                [cell.contentView addSubview:tetherSw];
                cell.textLabel.text = @"Tether";
                [tetherSw release];
            }
                break;
}

--

-(void)viewDidLoad{

    tetherSw = [[[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)] autorelease];
    tetherSw.tag = kDefaultSwTag;
    [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            case 3: 
            {
                 [cell addSubView:tetherSw];
            }
}
4

3 回答 3

1

没关系。您的单元格很复杂,因此您的视图滞后。

如果您想要性能,请避免使用UISwitch. 改为切换单元格的复选标记。事实上,只需避免任何花哨的表格视图单元子类或自定义背景,以减少视图层次结构的大小。

于 2010-01-17T07:20:10.607 回答
1

您是否正确地取消排队和重用单元格?

如果你重用一个单元格,比如@“SwitchCell”,它会优化很多东西,这会加快滚动速度。目前,将花费大量时间将开关添加到单元格的内容视图(布局视图等)并执行在单元格生命周期中只需要发生一次的其他任务,而不是每次滚动时出现新单元格时。

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

    // Create cell
    static NSString *CellIdentifier = @"SwitchCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UISwitch *tetherSw = [[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)];
        tetherSw.tag = kDefaultSwTag;
        [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:tetherSw];
        [tetherSw release];
    }

    // Setup for each cell
    cell.textLabel.text = @"Tether";
    NSNumber *tetherState = [[mobIntDict objectForKey:@"State"] intValue];
    NSNumber *currValState = [[NSNumber numberWithInt:1023] intValue];
    UISwitch *tetherSw = (UISwitch *)[cell.contentView viewWithTag: kDefaultSwTag];
    tetherSw.on = (tetherState == currValState);

    // Return
    return cell;

}

有关出队的更多信息,请参阅dequeueReusableCellWithIdentifier的文档。

此外,请确保您的任何单元格的子视图中都没有透明度。这会导致很多滞后。确保您添加的任何标签或其他任何内容都具有opaque = YES背景颜色集。

于 2010-01-17T07:33:21.563 回答
0

实际上,我的桌子设置得很好。一个可靠的恢复就可以了,我的应用程序在没有上述“LAG”的情况下运行

于 2010-01-25T00:49:28.740 回答