我正在做我的第一个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请让我知道。
你可以发布你的cellForRowAtIndexPath代码吗? – user1947561 2014-12-07 02:19:17