0

我在表格视图上设置了手势识别器。

  • 向右滑动,配件会变为刻度的图像
  • 向左滑动并更改为 V 形图像

如果点击一个单元格,它会加载一个本地 HTML 文件。

如果您向右滑动,勾号会按原样显示。但是,如果您随后点击单元格以查看 HTML 文件并返回表格视图,则图像将恢复为 V 形。

确保蜱虫保持原样的最佳方法是什么?


编辑

进一步的代码:

从'viewDidLoad':

UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(handleSwipeRight:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.tableView addGestureRecognizer:recognizer];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                       action:@selector(handleSwipeLeft:)];
//recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];


- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];

//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

//Check if index path is valid
if(indexPath)
{
    //Get the cell out of the table view
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    //Update the cell or model

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"disclosure.png"]];
}
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.tableView];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

if(indexPath)
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    // cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
}

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *simpleTableIdentifier = @"MFGCell";

MFGCell *cell = (MFGCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MFGCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

cell.itemTitle.text = [item objectAtIndex:indexPath.row];
cell.itemDescription.text = [description objectAtIndex:indexPath.row];
cell.itemImageView.image = [UIImage imageNamed:[icons objectAtIndex:indexPath.row]];

return cell;
}
4

3 回答 3

2

作为对用户滑动的反应,您应该存储用户的选择(例如,在类型的私有实例变量中NSMutableArray)。当用户返回表格视图时,您可以重用您的信息tableView:cellForRowAtIndexPath:来设置具有正确附件样式的单元格。

财产声明:

@property(nonatomic, retain) NSMutableArray* _accessoryStyle;

综合属性。然后将此代码段添加到底部handleSwipeLeft:以存储用户的选择:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
  [...]
  NSNumber* number = [numberWithInt:0];
  [_accessoryStyle replaceObjectAtIndex:indexPath.row withObject:number];
}

在底部添加一个类似的片段handleSwipeRight:

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
  [...]
  NSNumber* number = [numberWithInt:1];
  [_accessoryStyle replaceObjectAtIndex:indexPath.row withObject:number];
}

tableView:cellForRowAtIndexPath:

NSString* accessoryImageName;
NSNumber* number = [_accessoryStyle objectAtIndex:indexPath.row];
switch ([number intValue])
{
  case 0:
    accessoryImageName = @"disclosure.png";
    break;
  case 1:
    accessoryImageName = @"tick.png";
    break;
  default:
    // replace with your error handling code
    return nil;
}
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:accessoryImageName]];

为了使所有这些工作,您需要_accessoryStyle使用与您希望表格视图具有单元格的相同数量的元素来初始化数组。例如,在您的视图控制器中viewDidLoad

- (void) viewDidLoad
{
  [super viewDidLoad];

  self._accessoryStyle = [NSMutableArray arrayWithCapacity:0];
  NSNumber* defaultAccessoryStyle = [numberWithInt:0];
  int numberOfRows = 17;  // get the real number from somewhere
  for (int index = 0; index < numberOfCells; ++index)
    [_accessoryStyle addObject:defaultAccessoryStyle];
}

为了平衡这一点,您需要添加

- (void) viewDidUnload
{
  [super viewDidUnload];
  self._accessoryStyle = nil;
}

还有很大的改进空间:

  • 找到更好的变量名
  • 对不同样式使用枚举,而不仅仅是硬编码数字 0 和 1
  • 不要UIImageView为每个表格视图单元分配一个新的,只需分配其中两个并根据附件样式使用正确的一个
于 2012-12-28T18:17:13.383 回答
0

对于您的问题,存在一个潜在的逻辑问题,因为当它不应该触发向左滑动事件时,或者视图只是被卸载并重置为默认值。看看你是否可以在事件触发时记录;否则应保留视图的状态。另外,我要做的是添加一个额外的状态变量,就像int currentCellState您在输入不同状态时更改以跟踪您的状态一样。然后在您viewDIdLoad确保您的所有数据和您的视图是同步的,即值currentCellState匹配您的视图的状态。

于 2012-12-28T18:22:33.347 回答
0

最好的方法是将您拥有的图像/按钮放在一个数组中,每次加载视图时都会显示选择了索引的项目。为了做到这一点,应该将 swipeMethode 修改为类似这样

-(void)swipeMethod: (UISwipeGestureRecognizer *) sender
{
    if(sender.direction == 
        UISwipeGestureRecognizerDirectionLeft && index < [myArray count]){   
        [self setSelectedIndex:index+1  animated:YES];  
        index++;
    }else if (sender.direction == UISwipeGestureRecognizerDirectionRight && index > 0) {
        [self setSelectedIndex:index-1 animated:YES]; 
        index--;
    }else {
        return;
    }
}

在 viewDidLoad 添加以下代码:

    leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeMethod:)];
    [leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
    [self.tableView addGestureRecognizer:leftRecognizer];

    rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeMethod:)];
    [rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
    [self.tableView addGestureRecognizer:rightRecognizer];
于 2012-12-28T19:20:52.573 回答