代码之家  ›  专栏  ›  技术社区  ›  Jon Romero

iPhone的UITableViewCellAccessoryCheckMark

  •  3
  • Jon Romero  · 技术社区  · 6 年前

    CheckMarkAccessoryView UITableView 。当我在表中选择一行时,我可以得到该行的选中标记,但随机地,表中的其他一些行也会得到选中标记。我只希望该单元格得到选中标记马克。怎么了我能做这个吗?我正在为一个独家名单。名单下面是我使用的代码。

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {   
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {   
        if(tableView==tableView1)
        {
            return […arraycount1…. ];
        }
        else 
        {   
            return […arraycount2…. ];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cell";
    
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        { 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }
        else 
        {
            UIView* subview;
            while ((subview = [[[cell contentView] subviews] lastObject]) != nil)
                [subview removeFromSuperview];
        }
    
        if(tableView == tableView1)
        {
            cell.textLabel.text=[array1 objectAtIndex:indexPath.row];
        }
        else                    //Tableview2
        {
            cell.textLabel.text=[array2 objectAtIndex:indexPath.row];       
        }
    
        cell.textLabel.font=[UIFont fontWithName:@"Georgia" size:20];
        cell.textLabel.textAlignment=UITextAlignmentLeft;
        [cell setSelectedBackgroundView:border];    
        return cell;
    
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {   
    
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastindex]; 
        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark); 
        {   
            oldCell.accessoryType = UITableViewCellAccessoryNone;   
        }
    
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];     
    
        if (newCell.accessoryType == UITableViewCellAccessoryNone) 
        {   
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;  
        }   
    }
    

    请纠正我哪里出了问题。

    4 回复  |  直到 7 年前
        1
  •  2
  •   Jimmy    11 年前

    两个if语句相互抵消,只需添加如下内容:

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastindex]; 
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) 
    {   
        oldCell.accessoryType = UITableViewCellAccessoryNone;   
    }
    else
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];     
    
        if (newCell.accessoryType == UITableViewCellAccessoryNone) 
        {   
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;  
        }  
    }
    
        2
  •  4
  •   Sean Aitken    9 年前
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        for (id algoPath in [tableView indexPathsForVisibleRows])
        {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:algoPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
    
            if(algoPath == indexPath)
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        } 
    }
    

    我尝试了不同的方法,但都没有成功。现在我迭代所有索引,将所有附件设置为无,然后将当前附件设置为accessoryCheckmark。不是最干净的方法,但工作。

        3
  •  3
  •   grahamparks    14 年前

    UITableView在滚动表格时重用单元格对象,因此将单元格设置为显示复选标记意味着在重用时也会有复选标记

    您需要将每一行的状态存储在一个独立于单元格的数组中(或者如果您一次只需要检查一个单元格,只需在某个位置存储一个索引),然后从该数组中读取并设置适当的 accessoryType cellForRowAtIndexPath: 方法。

    (或者,您可以通过移除 dequeueReusableCellWithIdentifier:

        4
  •  1
  •   Muruganandam.S    9 年前

    我想你正在寻找这些方法!

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section!=0) {
            [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    
    //didDeselect method in table view for removing previous checkmark
    
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section!=0)
        {
            [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        }
    
    }