私はobjective-cでかなり新しく、UItableviewアプリをやろうとしています。全体の概念は、両方のテーブルビューの2つのビューがあるということです。最初のView Controllerには月があり、押している月に応じて、2番目のView Controllerで整数(int currentMonth)を変更します。2 番目のビュー コントローラーでは、テーブル ビューに動物を表示します。動物は、「狩猟可能」であるかどうかのみを表示し、「狩猟可能」である期間も表示する必要があり、そのためのコードを作成しましたが、機能します。
問題は、私の現在のコードでは、animalArray からオブジェクトを削除し、cellForRowAtIndexPath のテーブルビューのデータをリロードしているため、スクロールが非常に遅くなることです。
私は他の解決策を考え出そうとしましたが、これまでのところ運が悪いので、誰かが私を正しい方向に押し上げてくれることを願っています.
- (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"];
}
//これがスクロールを遅くする原因です。
if ([animalDetailLabel.text isEqual:@"Ej Jaktbar"]) {
[appDelegate.animalArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
return cell;
}
コードを変更する方法はありますか?