[英]UItableView performance issue after scrolling for a while
I have a UItableViewCell that I am creating programatically inside tableView:CellForRowAtIndexpath that is suffereing from poor performance after you have been scrolling for a while. 我有一个UItableViewCell,它是在tableView:CellForRowAtIndexpath内以编程方式创建的,在滚动了一段时间后,性能下降。
I initally had it as a custom UITableViewCell inside InterfaceBuilder, However I have no programtically created this view, which has increased the performance by about 40% however after scrolling say 40 - 100 lines its starts to get very glitchy.. I was thinking of working out how to cache the height of the UITableView of Cells however I am now woundering if its a fundimental error in my code. 我最初将它作为InterfaceBuilder中的自定义UITableViewCell使用,但是我没有以编程方式创建此视图,该视图将性能提高了约40%,但是在滚动显示40-100行后,它开始变得非常毛刺。出了如何缓存单元格UITableView的高度的方法,但是如果我的代码中出现基本错误,我现在就很痛苦。 For instance mybe my deque is not working?
例如mybe我的双端队列不起作用? I am not sure how to check this and was hoping for some help in diagnosing my issues.
我不确定如何检查此问题,并希望在诊断我的问题时获得帮助。
This is what my tableview method looks like 这就是我的tableview方法的样子
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([sortedItemsArray count] > 0) {
NSDictionary *currentInstallDictionary = [sortedItemsArray objectAtIndex:indexPath.row];
NSNumber *tempDUP = [currentInstallDictionary objectForKey:@"dup"];
NSInteger myInteger = [tempDUP integerValue];
if (myInteger == 0) {
//assign vals to labels
areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -2.0, 140.0, 35.0)];
NSString *areaString = [currentInstallDictionary objectForKey:@"area"];
if ((NSNull *) areaString != [NSNull null]) {
areaLabel.text = areaString;
} else {
areaLabel.text = @" ";
}
areaLabel.lineBreakMode = NSLineBreakByCharWrapping;
areaLabel.numberOfLines = 0;
[areaLabel sizeToFit];
[cell addSubview:areaLabel];
// removed other UIlabel that were here for readability
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(450.0, 5.0, 140.0, 35.0)];
NSString *descriptionString = [currentInstallDictionary objectForKey:@"partDesc"];
if ((NSNull *) descriptionString != [NSNull null]) {
descriptionLabel.text = descriptionString;
} else {
descriptionLabel.text = @" ";
}
descriptionLabel.lineBreakMode = NSLineBreakByCharWrapping;
descriptionLabel.numberOfLines = 0;
[descriptionLabel sizeToFit];
[cell addSubview:descriptionLabel];
shipmentLabel = [[UILabel alloc] initWithFrame:CGRectMake(605.0, 5.0, 140.0, 35.0)];
NSString *shipString = [currentInstallDictionary objectForKey:@"totShip"];
if ((NSNull *) shipString != [NSNull null]) {
shipmentLabel.text = shipString;
} else {
shipmentLabel.text = @" ";
}
shipmentLabel.lineBreakMode = NSLineBreakByCharWrapping;
shipmentLabel.numberOfLines = 0;
[shipmentLabel sizeToFit];
[cell addSubview:shipmentLabel];
quantityALabel = [[UILabel alloc] initWithFrame:CGRectMake(755.0, 5.0, 50.0, 35.0)];
NSString *totDryQtyString = [currentInstallDictionary objectForKey:@"totDryQty"];
if ((NSNull *) totDryQtyString != [NSNull null]) {
quantityALabel.text = totDryQtyString;
} else {
quantityALabel.text = @" ";
}
quantityALabel.lineBreakMode = NSLineBreakByCharWrapping;
quantityALabel.numberOfLines = 0;
[quantityALabel sizeToFit];
[cell addSubview:quantityALabel];
quantityBLabel = [[UILabel alloc] initWithFrame:CGRectMake(815.0, 5.0, 50.0, 35.0)];
NSString *totInsQtyString = [currentInstallDictionary objectForKey:@"totInsQty"];
if ((NSNull *) totInsQtyString != [NSNull null]) {
quantityBLabel.text = totInsQtyString;
} else {
quantityBLabel.text = @" ";
}
quantityBLabel.lineBreakMode = NSLineBreakByCharWrapping;
quantityBLabel.numberOfLines = 0;
[quantityBLabel sizeToFit];
[cell addSubview:quantityBLabel];
NSString *tDStateAString = [currentInstallDictionary objectForKey:@"totDryStateA"];
NSString *tDStateBString = [currentInstallDictionary objectForKey:@"totDryStateB"];
fitButtonImage = [UIButton buttonWithType:UIButtonTypeCustom];
fitButtonImage.frame = CGRectMake(888.0, 5.0, 35.0, 35.0);
if (([tDStateAString isEqualToString:@"T"]) && ([tDStateBString isEqualToString:@"W"])) {
UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
[fitButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
[cell addSubview:fitButtonImage];
} else if (([tDStateAString isEqualToString:@"T"]) && ([tDStateBString isEqualToString:@"R"])) {
UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
[fitButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
[cell addSubview:fitButtonImage];
} else {
if (![quantityALabel.text isEqualToString:@"0"]) {
UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
[fitButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
fitButtonImage.userInteractionEnabled = YES;
} else {
fitButtonImage.userInteractionEnabled = NO;
}
}
NSString *tIStateAString = [currentInstallDictionary objectForKey:@"totInstStateA"];
NSString *tIStateBString = [currentInstallDictionary objectForKey:@"totInstStateB"];
insButtonImage = [UIButton buttonWithType:UIButtonTypeCustom];
insButtonImage.frame = CGRectMake(968.0, 5.0, 35.0, 35.0);
if (([tIStateAString isEqualToString:@"T"]) && ([tIStateBString isEqualToString:@"W"])) {
UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
[insButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
[cell addSubview:insButtonImage];
} else if (([tIStateAString isEqualToString:@"T"]) && ([tIStateBString isEqualToString:@"R"])) {
UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
[insButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
[cell addSubview:insButtonImage];
} else {
UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
[insButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
[cell addSubview:insButtonImage];
}
//add lines to cell
lineBreakOneView = [[UIView alloc] initWithFrame:CGRectMake(140.0, 0.0, 0.5, 44.0)];
lineBreakOneView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:lineBreakOneView];
lineBreakTwoView = [[UIView alloc] initWithFrame:CGRectMake(290.0, 0.0, 0.5, 44.0)];
lineBreakTwoView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:lineBreakTwoView];
lineBreakThreeView = [[UIView alloc] initWithFrame:CGRectMake(440.0, 0.0, 0.5, 44.0)];
lineBreakThreeView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:lineBreakThreeView];
lineBreakFourView = [[UIView alloc] initWithFrame:CGRectMake(590.0, 0.0, 0.5, 44.0)];
lineBreakFourView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:lineBreakFourView];
lineBreakFiveView = [[UIView alloc] initWithFrame:CGRectMake(745.0, 0.0, 0.5, 44.0)];
lineBreakFiveView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:lineBreakFiveView];
lineBreakSixView = [[UIView alloc] initWithFrame:CGRectMake(805.0, 0.0, 0.5, 44.0)];
lineBreakSixView.backgroundColor = [UIColor lightGrayColor];
lineBreakSixView.alpha = 0.3;
[cell addSubview:lineBreakSixView];
lineBreakSevenView = [[UIView alloc] initWithFrame:CGRectMake(865.0, 0.0, 0.5, 44.0)];
lineBreakSevenView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:lineBreakSevenView];
lineBreakEightView = [[UIView alloc] initWithFrame:CGRectMake(945.0, 0.0, 0.5, 44.0)];
lineBreakEightView.backgroundColor = [UIColor lightGrayColor];
lineBreakEightView.alpha = 0.3;
[cell addSubview:lineBreakEightView];
return cell;
}
}
return cell; // this shouldnt happen if the data is correct
}
There are actually a few more labels in there also.. so its quite a decently complecated UITableViewCell which I would like to add a few more images too etc... but before I can do that I will need help sorting out the performance issues I am suffering from. 实际上那里还有更多的标签..所以它是一个相当不错的UITableViewCell,我也想添加一些图像等等。但是在我这样做之前,我需要帮助来解决性能问题。正在遭受。
any ideas or help solving the scroll performance for my UITableView would be greatly appreciate. 任何想法或帮助解决UITableView的滚动性能将不胜感激。
Things like: [cell addSubview:areaLabel];
像这样的东西:
[cell addSubview:areaLabel];
(where areaLabel = [[UILabel alloc] init..
) are not ok inside cellForRowAtIndexPath
, unless you're doing it inside the if (cell == nil)
. (其中
areaLabel = [[UILabel alloc] init..
)在cellForRowAtIndexPath
内部cellForRowAtIndexPath
,除非您在if (cell == nil)
。 In your case, you're creating and adding labels continously to your cells, so after scrolling a while, your actual cells will contain a multitude of labels/subviews (some will probably be hidden behind others). 在您的情况下,您要连续创建标签并将其添加到单元格,因此在滚动一会儿之后,您的实际单元格将包含大量标签/子视图(有些可能隐藏在其他标签/子视图之后)。
You need to create and add those subviews just once, and afterwards only set/refresh their content. 您只需要创建和添加这些子视图一次,然后仅设置/刷新它们的内容。
As @Fogmeister mentioned, it's best if you create a custom cell instead (separate class in which you do all your customization). 正如@Fogmeister提到的,最好创建一个自定义单元格(在其中进行所有自定义的单独类)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.