应用程序视图感觉像是“滞后”

发布于 2024-08-18 06:56:59 字数 1608 浏览 5 评论 0原文

我的应用程序中的大多数视图都是 UIViewController 内的 UITableVlew。我的应用程序在尝试滚动表格时感觉有点滞后。我想知道(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];
            }
}

Most of the views in my app are UITableVlews inside a UIViewController. My App feels like it's lagging when trying to scroll through the tables. I was wondering (1.) if it is better to create the cell objects in the table view, or create them at runtime and add them to the cells subview?

examples:

- (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;
}

-OR-

-(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];
            }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

再浓的妆也掩不了殇 2024-08-25 06:56:59

没关系。您的细胞很复杂,因此您的视野会滞后。

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

It doesn't matter. Your cell is complex and therefore your view lags.

If you want performance, avoid the UISwitch. Toggle the cell's checkmark instead. In fact, just avoid any fancy table view cell subclasses or custom backgrounds, to reduce the size of the view hierarchy.

山色无中 2024-08-25 06:56:59

您是否正确地使单元出队并重用?

如果您重用一个单元格(例如@“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 和背景颜色集。

Are you properly de-queuing and reusing cells?

It would optimise things a lot if you reused a cell, say a @"SwitchCell", that would speed up scrolling a lot. Currently a lot of time will be spent adding the switch to the cell's content view (laying out of views etc,) and performing other tasks that only need to happen once in a cells lifetime, instead of every time a new cell appears while scrolling.

- (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;

}

See the docs for dequeueReusableCellWithIdentifier for more information on dequeuing.

Also, make sure that there is no transparency in any of your cell's subviews. This causes a lot of lagging. Make sure any labels or anything else you add has opaque = YES and a background color set.

孤者何惧 2024-08-25 06:56:59

事实上,我的桌子设置得很好。可靠的恢复成功了,我的应用程序运行时没有上述的“LAG”

Actually, my tables are setup just fine. A solid restore did the trick, and my app runs without the afore-mentioned "LAG"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文