2014-12-07 27 views
0

我正在做我的第一个iOS应用程序,我有一个UITableView列表的UITableViewCells。每个UITableViewCells有两个标签:在UITableViewCell TapGesture导致其他单元格已更改状态

@property (weak, nonatomic) IBOutlet UILabel *upvote; 
@property (weak, nonatomic) IBOutlet UILabel *downvote; 

这些标签都有一个手势识别器。当轻击标签时,两个标签都会通过动画将其alpha设置为0。

-(void) handleSingleTapGesture: (UITapGestureRecognizer *)gestureRecognizer 
{ 
    UILabel *vote = (UILabel*)[gestureRecognizer view]; 
    AppCell *appCell = (AppCell*)vote.superview.superview; 

    [UIView animateWithDuration: 1.0 animations:^(void) 
    { 
     appCell.upvote.alpha = 0; 
     appCell.downvote.alpha = 0; 
    } 
} 

这里是cellForRawAtIndexPath方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    AppCell *cell = (AppCell *)[tableView dequeueReusableCellWithIdentifier:@"AppCell"]; 
    App *app (self.apps)[indexPath.row]; 

    /** setting the text/fonts/alpha for other labels I get from a HTTP POST request. Did not include the code since not relevant */ 

    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)]; 

    cell.upvote.font = [UIFont fontWithName:@"Roboto-Light" size:14]; 
    [cell.upvote setUserInteractionEnabled:YES]; 
    [cell.upvote addGestureRecognizer:singleTapGestureRecognizer]; 

    *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)]; 

    cell.downvote.font = [UIFont fontWithName:@"Roboto-Light" size:14]; 
    [cell.downvote setUserInteractionEnabled:YES]; 
    [cell.downvote addGestureRecognizer:singleTapGestureRecognizer]; 

} 

它的工作原理,因为它应该。当我点击文本时,它们都消失了。但是,似乎在向下滚动某些未来单元格时没有UPVOTE/DOWNVOTE文本。

我非常有信心,它与单元的重用有关,但我不知道如何解决它。

我已经查了潜在的解决方案,我似乎找不到多少。我唯一能找到的就是将手势识别器添加到UITableView而不是UITableViewCell,这样更有效率。我不能这样做,因为我不希望在单元格被点击时发生操作,只有当两个标签中的一个被点击时才会发生。

如果需要更多信息/有一个解决方案,我错过了StackOverflow请让我知道。

+0

你可以发布你的cellForRowAtIndexPath代码吗? – user1947561 2014-12-07 02:19:17

回答

1

正如你所猜测的,它可能与单元的重用有关。在cellForRowAtIndexPath方法的评论说,它处理的字母,但没有在该方法的α的设定,所以...

当一块电池可重复使用,upvotedownvote标签alpha是不管它上一次的设置, (默认为1,当创建UILabel时)或显式地(由手势处理器设置为0)。

您需要跟踪每个单元格的投票,并在cellForRowAtIndexPath方法中适当地设置alpha。

+0

我的意思是它处理alpha(通过设置初始alpha)。对困惑感到抱歉。这是否意味着它应该具有诸如'if(voteed)app.upvote.alpha = 0; else app.upvote.alpha = 1;'? – 2014-12-07 02:58:32

+0

@WilliamBingHua - 是的,类似的东西应该工作。 – bobnoble 2014-12-07 04:12:43

相关问题