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.
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. For instance mybe my deque is not working? 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
- (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.
any ideas or help solving the scroll performance for my UITableView would be greatly appreciate.
Things like: [cell addSubview:areaLabel];
(where areaLabel = [[UILabel alloc] init..
) are not ok inside cellForRowAtIndexPath
, unless you're doing it inside the 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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.