Im quite new in objective-c and im trying to do a UItableview app. The whole concept is that i have two views, both tableviews. In the first view controller I have months and depending on which month you are pressing Im changing an integer (int currentMonth) in the second view controller. In the second view controller I want to present a table view with animals. The animals should only show if they are "huntable" and also present how long they are "huntable" and Ive written code for that and it works.
The problem is that with my current code Im deleting objects out of the animalArray and reloading the data for the tableview in cellForRowAtIndexPath and that makes scrolling really slow.
Ive tried to come up with other solutions but so far no luck so I hope someone could push me in the right direction.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"DjurCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//skapar en variabel av Appdelegate för att komma åt arrayen med djur.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
if (cell== nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
//Skapar en label och länkar den med storyboard.
UILabel *animalNameLabel = (UILabel *)[cell viewWithTag:104];
UILabel *animalDetailLabel = (UILabel *)[cell viewWithTag:102];
NSString *strmonth;
switch (self.currentMonth) {
case 0:
strmonth=@"Juli";
break;
case 1:
strmonth=@"Augusti";
break;
case 2:
strmonth=@"September";
break;
case 3:
strmonth=@"Oktober";
break;
case 4:
strmonth=@"November";
break;
case 5:
strmonth=@"December";
break;
case 6:
strmonth=@"Januari";
break;
case 7:
strmonth=@"Februari";
break;
case 8:
strmonth=@"Mars";
break;
case 9:
strmonth=@"April";
break;
case 10:
strmonth=@"Maj";
break;
case 11:
strmonth=@"Juni";
break;
case 12:
strmonth=@"Juli";
break;
default:
break;
}
//Algoritm för utskrivandet av hur lång tid en art är jaktbar. Om nuvarande månad är större än jaktstarten och mindre än jaktstoppet.
if ((self.currentMonth>[[appDelegate.animalArray objectAtIndex:indexPath.row]getjaktstartmonth])&&(self.currentMonth<[[appDelegate.animalArray objectAtIndex:indexPath.row]getjaktstopmonth])) {
animalNameLabel.text = [[appDelegate.animalArray objectAtIndex:indexPath.row]getArt];
animalDetailLabel.text = @"Jaktbar hela månaden";
}
//Om nuvarande månad är lika med jaktstarten.
else if(self.currentMonth==[[appDelegate.animalArray objectAtIndex:indexPath.row]getjaktstartmonth]){
animalNameLabel.text = [[appDelegate.animalArray objectAtIndex:indexPath.row]getArt];
animalDetailLabel.text = [NSString stringWithFormat:@"Jaktbar från och med den %i:e %@",[[appDelegate.animalArray objectAtIndex:indexPath.row]getjaktstartday],strmonth];
}
//Om nuvarande månad är lika med jaktstoppet.
else if(self.currentMonth==[[appDelegate.animalArray objectAtIndex:indexPath.row]getjaktstopmonth]){
animalNameLabel.text = [[appDelegate.animalArray objectAtIndex:indexPath.row]getArt];
animalDetailLabel.text = [NSString stringWithFormat:@"Jaktbar till och med den %i:e %@",[[appDelegate.animalArray objectAtIndex:indexPath.row]getjaktstopday],strmonth];
}
//I övriga fall
else{
animalNameLabel.text = [[appDelegate.animalArray objectAtIndex:indexPath.row]getArt];
animalDetailLabel.text = [NSString stringWithFormat:@"Ej Jaktbar"];
}
//This is what makes the scrolling slow.
if ([animalDetailLabel.text isEqual:@"Ej Jaktbar"]) {
[appDelegate.animalArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
return cell;
}
Any ideas how I should change the code?