静态表视图单元格似乎具有相同的数据和单元格选择索引?

时间:2013-09-06 20:19:49

标签: ios objective-c uitableview

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

-(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;
}

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

由于

2 个答案:

答案 0 :(得分:0)

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

cell.textLabel.text行移出return cell;

正上方的if(){}块
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

答案 1 :(得分: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;

    ...