請幫我寫好的代碼的UITableView細胞再利用,低內存警告,應用程序崩潰的設備加載圖像
我的情況後:從自定義圖像陣列的tableview 加載圖片(大小可1-1000或可能更多) - 完成
放置的圖像上的按鈕,完成(不知道正確的方式,但做工精細)
的幫助下獲取圖像標記按鈕,完成
不要在小區負載額外的圖像(假設在4圖像視圖一個細胞和26個圖像) - 完成
我在UItableView載入圖像,但應用程序只在設備崩潰,在模擬器中正常工作。我google了一下,發現一些答案,重新使用單元格相應地在一定程度上修改了我的代碼。但我仍然不確定圖像加載和崩潰。有些時候,圖像少了,滾動時就會崩潰,而在表格視圖中加載圖像之後的某段時間。 我接近我的解決方案,但沒有得到好的結果。請幫忙!!
請檢查我的代碼,如果需要修改它
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];
// static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
NSInteger imageIndex = [indexPath row] * 4;
NSInteger size = [imageArray count];
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
imageView1.image = [imageArray objectAtIndex:imageIndex];
aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton1 setTag:imageIndex];
[aButton1 setImage:imageView1.image forState:UIControlStateNormal];
aButton1.frame = CGRectMake(0, 0, 80,80);
[aButton1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aButton1];
if (size == imageIndex +1) {
return cell;
}
imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(80, 0, 80,80)];
// imageView2.tag= tagValue+2000;
imageView2.image = [imageArray objectAtIndex:imageIndex+1];
[cell.contentView addSubview:imageView2];
UIButton* aButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton2 setTag:imageIndex+1];
[aButton2 setImage:imageView2.image forState:UIControlStateNormal];
aButton2.frame = CGRectMake(80, 0, 80,80);
[aButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aButton2];
if (size == imageIndex + 2) {
return cell;
}
imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(160, 0, 80,80)];
imageView3.image = [imageArray objectAtIndex:imageIndex+2];
UIButton* aButton3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton3 setTag:imageIndex+2];
[aButton3 setImage:imageView3.image forState:UIControlStateNormal];
aButton3.frame = CGRectMake(160, 0, 80,80);
[aButton3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aButton3];
if (size == imageIndex + 3) {
return cell;
}
imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(240, 0, 80,80)];
imageView4.image = [imageArray objectAtIndex:imageIndex+3];
UIButton* aButton4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton4 setTag:imageIndex+3];
[aButton4 setImage:imageView4.image forState:UIControlStateNormal];
aButton4.frame = CGRectMake(240, 0, 80,80);
[aButton4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aButton4];
}
else
{
NSLog(@"old one");
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
使用弧並在tableview中加載10-10行,而不是一次加載全部。 – 2013-04-26 06:31:42
首先,如果條件ieif(cell == nil)cell = [[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}然後編寫用於向imageview提供圖像的代碼和按鈕。我不知道你爲什麼提供如果(size == imageIndex + 3)return cell;}條件?你能詳細說明一下嗎? – 2013-04-26 06:42:23
@NuzhatZari Zari不要在單元格中加載額外的圖像(假設在一個單元格中有4個圖像視圖和26個圖像) - 完成 和ganesh manoj我無法使用arc我的項目沒有使用它,請建議一些更好的解決方案 – DreamWatcher 2013-04-26 06:45:45