开发者

Problem with tableview cell

I am using these following codes to display multiple images on a table view. One row need to display 4 only images

- (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];
    }

}

On the button click wich will display table view I wrote the below code

-(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:AutoCompleteRow开发者_JS百科Identifier] 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];
    }
}

In this code when I displayed 4 images it will display 4. If I displayed 3 images after displaying 4 images, 3 images are displaying but still 4 th image is presenting in table view. How can I fix this. Please can any one help me...

The main problem is, an image view that displayed before is not removing from the cell.


Table cells can be (and in your code, are) cached and reused - which means that for every call to cellForRowAtIndexPath:: you should make sure that the cell you are returning is correct (has the correct images). In your case, I would simply clear the cell of all subviews/image views and recreate them for every fetch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜