ios - UItableViewCell 重复

标签 ios uitableview reload

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

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

}

最佳答案

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

所以

  //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 block 之后执行此操作

        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;

希望这有帮助..

关于ios - UItableViewCell 重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11675033/

相关文章:

iOS Facebook SDK "user cancelled login"在设备上运行应用程序时?

ios - 类的实例未在 Swift 中打印其值

ios - 使用performSegueWithIdentifier/编程方式快速更改 View

objective-c - 奇怪的错误 : insertRowsAtIndexPaths in UITableView crashes with NSInternalInconsistencyException

ios - "cellForRowAtIndexPath"未调用 alertviewcontroller 内的 TableView

ios - 在 UISearchDisplayController 后面重新加载 UITableView

Python3 - 不能在 __import__ 对象上调用 reload()?

ios - 将 subview 的子类添加到 UIView 的子类

UITableViewCell 的 iOS 商店选择?

JQuery DataTables - AJAX 重新加载如何在没有返回任何内容时捕获