【发布时间】:2014-11-28 14:24:54
【问题描述】:
我使用以下代码创建了一个带有滑动手势的应用程序。手势应仅适用于特定行。第一次加载时它工作得很好。从第二次开始,手势功能对每一行都有效。
我的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GoalDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoalDetailsCell"];
if(cell == nil)
{
cell = [[GoalDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoalDetailsCell"];
}
NSMutableDictionary *cellData = [self.databaseCall transactionFromDatabase:indexPath.row goalId:self.goalId andStageId:self.stageId];
cell.goalDescription.text = self.goalName;
cell.actionGoalDescription.text = self.goalName;
cell.tipsDescription.text = [cellData objectForKey:@"tipsDescription"];
UIImage *cellImage = [UIImage imageNamed:[cellData objectForKey:@"categoryImage"]];
NSLog(@"%@", [cellData objectForKey:@"cardType"]);
if([[cellData objectForKey:@"cardType"] isEqualToString:@"2"])
{
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftMethod:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell.cardDetails addGestureRecognizer:swipeLeftGesture];
[cell.tryThisButton addTarget:self action:@selector(tryThisButtonPressed:) forControlEvents:UIControlEventTouchDown];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightMethod:)];
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell.actionCardReminder addGestureRecognizer:swipeRightGesture];
[cell.previousButton addTarget:self action:@selector(previousButtonPressed:) forControlEvents:UIControlEventTouchDown];
cell.tryThisButton.hidden = NO;
[cell.tryThisButton setImage:[UIImage imageNamed:@"trythis.jpg"] forState:UIControlStateNormal];
[cell.actionCardType setImage:[UIImage imageNamed:@"toconsider.jpg"]];
cell.cardType.image = [UIImage imageNamed:@"toconsider.jpg"];
cell.cardTypeImage.hidden = YES;
cell.actionCardGoalName.text = self.goalName;
cell.actionGoalsImage.image = cellImage;
cell.actionCardReminder.layer.cornerRadius = 5;
cell.datePicker.layer.borderWidth = 0.2;
cell.datePicker.layer.borderColor = [[UIColor grayColor] CGColor];
}
else{
cell.tryThisButton.hidden = YES;
cell.cardTypeImage.hidden = NO;
cell.cardType.image = [UIImage imageNamed:@"goodtoknow.jpg"];
cell.cardTypeImage.image = [UIImage imageNamed:@"tips.jpg"];
}
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[cellData objectForKey:@"actionLink"]];
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, [str length])];
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, [str length])];
[cell.reminderSwitch addTarget:self action:@selector(customSwitchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.datePicker addTarget:self action:@selector(datePickerButtonPressed:) forControlEvents:UIControlEventTouchDown];
cell.weburl.attributedText = str;
cell.goalName.text = self.goalName;
cell.goalsImage.image = cellImage;
cell.cardDetails.layer.cornerRadius = 5;
NSLog(@"%d", indexPath.row);
// Configure the cell...
return cell;
}
提前致谢。
【问题讨论】:
-
尝试为每种细胞类型使用两个不同的类。
-
你能给我一些示例程序吗
-
我放了一些代码作为答案(因为太长了,不能作为评论发表)。