here is my problem: My UITableViewController lags when I scroll it. It has about 60 custom cells. However, on the simulator it works without any lags. Here is my code: CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize nameLabel,costLabel,giftPicture;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
nameLabel = [[UILabel alloc]init];
nameLabel.textAlignment = UITextAlignmentLeft;
nameLabel.textColor = [UIColor colorWithRed:25/256.0 green:98/256.0 blue:52/256.0 alpha:1.0];
costLabel = [[UILabel alloc]init];
costLabel.textAlignment = UITextAlignmentLeft;
giftPicture = [[UIImageView alloc]init];
[self.contentView addSubview:nameLabel];
[self.contentView addSubview:costLabel];
[self.contentView addSubview:giftPicture];
costLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background4"]];
nameLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background4"]];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10,10, 50, 50);
giftPicture.frame = frame;
frame= CGRectMake(boundsX+70 ,10, 350, 25);
nameLabel.frame = frame;
frame= CGRectMake(boundsX+70 ,40, 350, 18);
costLabel.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
List.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithFrame:CGRectZero];
}
// Configure the cell...
NSString *cellValue = [array1 objectAtIndex:indexPath.row];
//cell.textLabel.text = cellValue; // загаловок клетки
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; // сделал чтобы клетка была со стрелочкой
cell.nameLabel.text = cellValue;
NSDictionary *mainDictionary = [[NSDictionary alloc]init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
if([currentLanguage isEqualToString:@"en"])
{
mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"txt"]];
}
NSEnumerator *enumerator = [mainDictionary objectEnumerator];
id returnValue;
while ((returnValue = [enumerator nextObject]))
{
if([cell.nameLabel.text isEqualToString:[returnValue valueForKey:@"name"]] )
{
cell.animalPicture.image = [UIImage imageNamed:[returnValue valueForKey:@"icon"]];
cell.costLabel.text = [NSString stringWithFormat:@"$%@ - $%@",[returnValue valueForKey:@"minAge"],[returnValue valueForKey:@"maxAge"]];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}
@end
I tried to make custom cells empty (remove labels and images), just because of curious, but it didn't change anything. Could you tell me please, what I should check to fix these lags? Thanks!