【问题标题】:UItableViewCells Are RepeatingUItableViewCells 重复
【发布时间】:2012-07-25 09:04:36
【问题描述】:

好的,我在每个单元格中创建了一个带有单选按钮的简单表格视图,这样做是为了查看单元格重复的原因。我将我的行数设置为高得离谱,以表明细胞确实重复。这个简单项目的目标是在解决这个问题时得出一个合理的结论,因为有几个关于这个主题的帖子都没有给出正确的结果。当用户在单元格中选择一个按钮时,该单元格并且只有该单元格应该受到影响。这是完整的代码。

    #import "faQViewController.h"

     @interface faQViewController ()

    @end

    @implementation faQViewController
    @synthesize button1,button2;

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }

     - (void)viewDidUnload
    {
     [super viewDidUnload];
     // Release any retained subviews of the main view.
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;


    }


    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath{
    static NSString *cellIdentifier =@"cell";
    button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    button1.frame = CGRectMake(0, 0, 22, 32);
    [button1 setImage:[UIImage imageNamed:@"radioOff.png"] forState:UIControlStateNormal];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {        
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
                 ] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
         [cell.contentView
          addSubview:button1];
    }

   // cell.imageView.image = [UIImage imageNamed:@"radioOff.png"];


    return cell;
}

    -(IBAction)buttonPressed:(id)sender{
    if ([sender imageForState:UIControlStateNormal ]== [UIImage imageNamed:@"radioOff.png"]){
    [sender setImage:[UIImage imageNamed:@"radioOn"] forState:UIControlStateNormal];
    }else {

        [sender setImage:[UIImage imageNamed:@"radioOff.png"] forState:UIControlStateNormal];
    }

}

【问题讨论】:

  • 到底是什么问题?单元格在 tableViews 中不重复,您正在重复使用单元格。
  • 您认为dequeueReusableCellWithIdentifier: 中的“可重用”一词是什么意思?
  • 这不是我第一次使用 tableViews,从它的外观来看,单元格正在被重用,问题是,假设我在单元格中有一个可编辑的对象,即按钮的图像。如果用户更改此可编辑对象,即图像,为什么当用户滚动出视图时单元格会重新加载并删除已编辑的图像。以及它不改变的解决方案是什么。

标签: ios uitableview reload


【解决方案1】:

您正在重复使用单元格,因此如果您不更改内容,您会看到其他行出现相同的单元格。由于你只在分配单元格时设置内容,所以当单元格被重用时,内容将保持不变

所以

  //Here you tell the tableView to re use a cell if one is available for reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   //if the cell is nil (none available for reuse)
    if (cell ==nil) {        
        //you create the cell and set its content
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
                 ] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
         [cell.contentView
          addSubview:button1];
    }
    //return the cell
   return cell;

如果您想根据行更改单元格内容,您应该在 cell==nil 块之后这样做

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
       //if the cell is nil (none available for reuse)
        if (cell ==nil) {        
            //you create the cell and set its content
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier
                     ] autorelease];
            }

          //set the content
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
             [cell.contentView
              addSubview:button1];

        //return the cell
       return cell;

希望这会有所帮助..

【讨论】:

  • 这会重新加载图像,因此当用户单击按钮图像以更改图像时,一旦您向下滚动到视图之外,它就会恢复为原始图像
【解决方案2】:

丹尼尔斯的回答帮助我解决了一些问题以解决我的问题。我的问题是我看到sections 0,1,2 是唯一创建的,但随后生成的second 3section 0 相同,这是我的设置:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"cell";

    CustomCell* cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"SECTION %d", indexPath.section]];

    NSLog(@"SECTION %d", indexPath.section);

    return cell;
}

我需要更改的是cellIndentifier,因为我将它们全部设置为相同的 ID:

NSString *cellIdentifier = [NSString stringWithFormat:@"cell-%d", indexPath.section];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多