0

基本上我需要两件事来处理我视图中的单元格,点击和点击并按住手势。我的点击并按住手势是这样工作的:

-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer
{
    NSLog(@"gestureRecognizer= %@",gestureRecognizer);

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
    {
        NSLog(@"longTap began");

        CGPoint p = [gestureRecognizer locationInView:self.tableView];

        NSIndexPath *indexPath = [myTable indexPathForRowAtPoint:p];
        if (indexPath == nil)
        {
            NSLog(@"long press on table view but not on a row");
        }
        else
        {
            NSLog(@"long press on table view at row %d", indexPath.row);

            switch (indexPath.row)
            {
                case 0:
                    del.tableRowNumber = 0;
                    break;
                case 1:
                    del.tableRowNumber = 1;
                    break;
                case 2:
                    del.tableRowNumber = 2;
                    break;
                case 3:
                    del.tableRowNumber = 3;
                    break;
                case 4:
                    del.tableRowNumber = 4;
                    break;
                case 5:
                    del.tableRowNumber = 5;
                    break;
            }
        }

        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"MealPlannerRecipeTypeViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

我需要这个手势来将单例类中的值设置为某个值,具体取决于选择的行。无论选择哪一行,此值始终为 0?!谁能告诉我为什么?

问题的第二部分是我的一个 tableview 代表看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    RecipeInfo *recipeInfo = recipeInfoArray[indexPath.row];
    cell.textLabel.text = recipeInfo.name;

    // Add long tap for the main tiles
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
    [cell addGestureRecognizer:longPressGesture];
    }

    return cell;
}

我的表格视图中的每一行都与第一行具有相同的信息?为什么是这样?

谢谢

4

2 回答 2

0

由于您使用的是可重复使用的单元格,并且您已经更改了 if() alloc-init 块中的 textLabel 。

cell.textLabel.text行移出 if() { } 块正上方return cell;

if (cell == nil) {
    cell = ...
}
    RecipeInfo *recipeInfo = recipeInfoArray[indexPath.row]
    cell.textLabel.text = recipeInfo.name;
    return cell;
}

编辑:以上是针对问题 2。

第一个问题,关于你的手势识别器......看看你的代码,你有

[gestureRecognizer locationInView:self.tableView];

但后来你写

[myTable indexPathForRowAtPoint

你有没有想过locationInView:self.tableView改变locationInView:myTable

于 2013-09-06T20:28:39.500 回答
0

仅在创建单元格时设置单元格值。“出队”可以返回一个已经实例化的单元格。我打算在括号中移动。

static NSString *CellIdentifier = @"Cell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    // Add long tap for the main tiles
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
    [cell addGestureRecognizer:longPressGesture];

    }
    RecipeInfo *recipeInfo = recipeInfoArray[indexPath.row];
    cell.textLabel.text = recipeInfo.name;

    ...
于 2013-09-06T20:30:57.070 回答