iPhone的UITableViewCellAccessoryCheckMark

时间:2010-09-04 06:21:19

标签: cocoa-touch uitableview

我在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 个答案:

答案 0 :(得分:4)

- (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。不是最干净的方法,而是工作。

答案 1 :(得分:3)

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

您需要将每行的状态存储在与单元格分开的数组中(或者如果您一次只需要一个单元格,只需在某处存储索引),然后从该数组中读取并设置相应的{ {1}}方法中的{1}}。

(或者您可以通过删除accessoryType调用来禁用单元格重用,但如果您有一个很长的列表要滚动,这对性能有害)

答案 2 :(得分:2)

两个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;  
    }  
}

答案 3 :(得分:1)

我认为你正在寻找这些方法!

-(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;
    }

}