我使用以下代码在表格视图上显示多个图像。一行需要显示4个图像
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
//Resize auto complete table based on how many elements will be displayed in the table
return (int)ceil([wordsInSentence count]/4.0);
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"entered forRowAtIndexPath");
if ((indexPath.row % 2) == 0) {
cell.backgroundColor = [UIColor whiteColor];
}
else {
cell.backgroundColor = [UIColor whiteColor];
}
}
点击按钮,将显示表格视图,我写了下面的代码
-(IBAction)setImageOnTableView
{
NSLog(@"entered setImageOnTableView");
j=1;
tableView.hidden=NO;
//Breaking the sentence into words and then placing it in an array
wordsInSentence=[[youSaid.text uppercaseString] componentsSeparatedByString:@" "];
[wordsInSentence retain];
[youSaid resignFirstResponder];
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSLog(@"entered cellForRowAtIndexPath");
//j=1;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
[cell.contentView addSubview:nil];
for (int i=0; i <= [wordsInSentence count]; ++i) {
UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 20, 70, 100)] autorelease] ;
imageView1.tag = i+1;
[imageViewArray insertObject:imageView1 atIndex:i];
[cell.contentView addSubview:imageView1];
//[imageView1 release];
}
}
int photosInRow;
if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([wordsInSentence count] % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = [wordsInSentence count] % 4;
}
for ( int i = 1; i <=photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
[self **showImage**:imageView];
}
return cell;
}
//method used to show the images on the table view by checking the category
//and images in the category
-(void)**showImage**:(UIImageView *)imageView
{
NSLog(@"entered showImage");
//this method will check if more than 1 image is present in the category
if([self searchImagesInSameCategory:[wordsInSentence objectAtIndex:j-1]]>1)
{
UIButton *descriptiveButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[descriptiveButton setFrame:CGRectMake(50, 0, 30, 30)];
[imageView setUserInteractionEnabled:TRUE];
[descriptiveButton addTarget:self action:@selector(showPopOver1:) forControlEvents:UIControlEventTouchUpInside];
descriptiveButton.tag=j;
[imageView addSubview:descriptiveButton];
globalString=[wordsInSentence objectAtIndex:j-1];
}
}
在此代码中,当我显示4个图像时,它将显示4.如果我在显示4个图像后显示3个图像,则显示3个图像,但仍然在表格视图中显示第4个图像。我怎样才能解决这个问题。请任何人帮助我......
主要问题是,之前显示的图像视图未从单元格中删除。
答案 0 :(得分:3)
表格单元格可以(并且在您的代码中)被缓存和重用 - 这意味着对于每次调用cellForRowAtIndexPath::
,您应该确保您返回的单元格是正确的(具有正确的图像)。在您的情况下,我只需清除所有子视图/图像视图的单元格,并为每次获取重新创建它们。