在我的應用程序中,我使用表視圖來顯示圖像。我在一行中顯示4個圖像。我能夠很好地顯示圖像。但我的問題是,當我滾動表視圖來查看圖像然後滾動發生在一個非常緩慢的速度,也陷入介於兩者之間。可能是什麼原因。我在這裏張貼我的代碼的一部分,請幫助。UITableView滾動問題
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog([NSString stringWithFormat:@" numberOfRowsInSection :%d",(int)ceil([wordsInSentence count]/4.0)]);
buttonIndex=1;
return (int)ceil([wordsInSentence count]/4.0);
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//return cell;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
for (int i=0; i < 4; i++) {
UIImageView *importMediaSaveImage;
NSString *name1;
NSString *name2;
NSString *name3;
NSString *name4;
switch (i) {
case 0:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 0)){
name1= [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 0)];
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name1 ofType:@"png" inDirectory:@"Images"]];
buttonIndex=((indexPath.row * 4)+ 0)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[cell.contentView addSubview:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
NSLog(name1);
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView setHidden:YES];
}
break;
case 1:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 1)){
name2 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 1)];
NSLog(name2);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name2 ofType:@"png" inDirectory:@"Images"]];
[cell.contentView addSubview:importMediaSaveImage];
buttonIndex=((indexPath.row * 4)+ 1)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
case 2:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 2)){
name3 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 2)];
NSLog(name3);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name3 ofType:@"png" inDirectory:@"Images"]];
[cell.contentView addSubview:importMediaSaveImage];
buttonIndex=((indexPath.row * 4)+ 2)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
case 3:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 3)){
[cell.contentView addSubview:importMediaSaveImage];
name4 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 3)];
NSLog(name4);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name4 ofType:@"png" inDirectory:@"Images"]];
buttonIndex=((indexPath.row * 4)+ 3)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
} //switch end
} //for loop end
return cell;
}
看看[這個](http://mobile.tutsplus.com/tutorials/iphone/uitableview-2/) – Sisu
我正在使用表視圖來連續加載多個圖像。但是當我滾動表視圖表視圖崩潰或重疊 –