views:

129

answers:

2

I have set the size of the tableView that I show as the popoverController as 4*rowheight. And I am using 12cells in the tableView. Each cell contains an image and a label. I can see all the cells by scrolling. Upto 5th cell its ok. After th2 5th cell, the label and the image that I am using in the first four cells are being repeated for the remaining cells. And If I select the cell, the result is accurately shown.

But when I again take the tableView, the image and labels are not accurate even for the first 5 cells. All are changed but the selection is giving the correct result. Can anyone help me??

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [self tableviewCellWithReuseIdentifier:CellIdentifier rowNumber:indexPath.row];
}
//tableView.backgroundColor = [UIColor clearColor];
return cell;
}


- (UITableViewCell *)tableviewCellWithReuseIdentifier:(NSString *)identifier rowNumber:(NSInteger)row
{   

 CGRect rect;
rect = CGRectMake(0.0, 0.0, 360.0, ROW_HEIGHT);
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:rect reuseIdentifier:identifier] autorelease];

UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.00, 10.00, 150.00, 100.00)];
myImageView.tag = IMAGE_TAG;
[cell.contentView addSubview:myImageView];
[myImageView release];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(170.00, -10.00, 170.00, 80.00)];
label.tag = LABEL_TAG;
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor blackColor]];
[label setFont:[UIFont fontWithName:@"AmericanTypewriter" size:22]];
[label setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:label];
[label release];

if (row == 0)
{
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:IMAGE_TAG];
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"cover_v.jpg"]];
    UILabel *mylabel = (UILabel *)[cell viewWithTag:LABEL_TAG];
    mylabel.text = [NSString stringWithFormat:@"COVER PAGE"];
}
   }
A: 

Ahh, wolverine you are doing a silly mistake:

Basically you are settings the value of the label in the

    if(cell==nil){
}

block. Hence the values are repeated because they are reused and not set for each cell.

You need to set the values outside this block with the help of indexpath and the change will reflect accordingly.

Hope this helps,

Thanks,

Madhup

Madhup
I got it man. Easiest way, I just deleted the first two lines in the function and made the third line UITableViewCell *cell = [[UITableViewCell alloc] init].
wolverine
A: 

You should provide imageView.image and label.text for all table:

UIImageView *imageView = (UIImageView *)[cell viewWithTag:IMAGE_TAG];
UILabel *mylabel = (UILabel *)[cell viewWithTag:LABEL_TAG];

if (row == 0)
{
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"cover_v.jpg"]];
    mylabel.text = [NSString stringWithFormat:@"COVER PAGE"];
}
else
{
    imageView.image = // ???
    mylabel.text = // ??
}

and of course, that code should be out of "if (cell == nil) {...}" frame.

indexless