应用程序视图感觉像是“滞后”
我的应用程序中的大多数视图都是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没关系。您的细胞很复杂,因此您的视野会滞后。
如果您想要性能,请避免使用
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.您是否正确地使单元出队并重用?
如果您重用一个单元格(例如@“SwitchCell”),它会大大优化事情,这会大大加快滚动速度。目前,将花费大量时间将开关添加到单元格的内容视图(布局视图等)并执行其他任务,这些任务只需要在单元格生命周期中发生一次,而不是每次滚动时出现新单元格。
请参阅文档 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.
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.事实上,我的桌子设置得很好。可靠的恢复成功了,我的应用程序运行时没有上述的“LAG”
Actually, my tables are setup just fine. A solid restore did the trick, and my app runs without the afore-mentioned "LAG"