2010-09-04 40 views
3

我在创建UITableView中的CheckMarkAccessoryView时遇到了问题。当我在表中选择一行时,我可以得到该行的选中标记,但随机表中的其他一些行也会得到复选标记。我只需要那个单元格就可以得到复选标记。我可以怎么做?我正在编写一个独占列表。以下是我用过的代码。iPhone的UITableViewCellAccessoryCheckMark

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

请问我在哪里错了。 在此先感谢所有的专家。

回答

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

的UITableView重用小区对象为您滚动表,所以设置单元格以显示一个勾号意味着它也将有重复使用时的复选标记

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

(或者你可以简单地通过删除dequeueReusableCellWithIdentifier:呼叫禁止小区重用,但是这是很糟糕的表现,如果你有很长的列表中滚动)

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

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

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

} 
相关问题