2014-09-20 70 views
0

我知道这个问题有很多答案。但它没有达到预期的结果。 didDEselectrow不起作用。我有一个UIImageView,我已经设置为在cellforRowAtIndex中隐藏True。并且当某人选择隐藏的任何行值设置为false时。使用自定义UITableViewCell在UITableView中执行单行选择?

**我的主要问题是当我选择另一行时,上一行状态不会改变,它也显示选中。我附上了我的代码所需的代码片段,请检查并帮助我完成此操作。 **

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    EventTypeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Event_Type" forIndexPath:indexPath]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    if (cell.selectionStatus) 
     [cell.selectEvent setHidden:NO]; 
    else 
     [cell.selectEvent setHidden:YES]; 

    [cell.eventTypeName setText:[eventTypeName objectAtIndex:indexPath.row]]; 

    return cell; 
} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
EventTypeTableViewCell *cell = (EventTypeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 

    if(cell.selectionStatus == TRUE) 
    { 
     cell.selectionStatus = FALSE; 
    } 
    else 
    { 
]  cell.selectionStatus = TRUE; 
    } 
    [tableView reloadData]; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    EventTypeTableViewCell *cell = (EventTypeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
    if(cell.selectionStatus == FALSE) 
    { 
     //Do your stuff 
     cell.selectionStatus = TRUE; 
    } 
    else 
    { 
     //Do your stuff 
     cell.selectionStatus = FALSE; 
    } 
    [tableView reloadData]; 
} 

回答

0

我怀疑didDeselect没有被调用,因为你允许在你的tableView中有多个选择。

您是否将表格视图的选择样式设置为“单选”?您可以做在Interface Builder /故事板或可以以编程方式做到这一点:

- (void) viewDidLoad { 
    self.tableView.allowsMultipleSelection = NO; 
} 

当您选择另一行会取消旧的选择这种方式。你也可以简化你的代码,每当单元格的选定状态发生变化时不调用[tableView reloadData],使所有内容看起来更好。我建议的做法是每当单元格的选择更改时,在EventTypeTableViewCell之内更改selectEvent的隐藏状态。

这使得你这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    EventTypeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Event_Type" forIndexPath:indexPath]; 

    [cell.eventTypeName setText:[eventTypeName objectAtIndex:indexPath.row]]; 

    return cell; 
} 

和你的EventTypeTableViewCell定义范围内的一些方法覆盖:

@implementation EventTypeTableViewCell 

/// Your code /// 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
    self.selectEvent.hidden = !selected; 
} 

@end 
0

试试这个::

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 
相关问题