0

我已将显示为 popoverController 的 tableView 的大小设置为 4*rowheight。我在 tableView 中使用了 12 个单元格。每个单元格包含一个图像和一个标签。我可以通过滚动查看所有单元格。直到第 5 个单元格都可以。在第 5 个单元格之后,我在前四个单元格中使用的标签和图像正在为剩余的单元格重复。如果我选择单元格,结果会准确显示。

但是当我再次使用 tableView 时,即使前 5 个单元格的图像和标签也不准确。一切都改变了,但选择给出了正确的结果。谁能帮我??

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

2 回答 2

1

啊,金刚狼你犯了一个愚蠢的错误:

基本上你是在设置标签的值

    if(cell==nil){
}

堵塞。因此,这些值被重复,因为它们被重复使用而不是为每个单元格设置。

您需要在 indexpath 的帮助下设置此块之外的值,并且更改将相应地反映出来。

希望这可以帮助,

谢谢,

马杜普

于 2010-05-07T11:19:14.203 回答
0

您应该为所有表格提供 imageView.image 和 label.text :

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 = // ??
}

当然,该代码应该在“if (cell == nil) {...}”框架之外。

于 2010-05-07T13:05:23.450 回答