Ios 静态表视图单元格似乎具有相同的数据和选定的单元格索引?
基本上,我需要两件事来处理我视图中的单元格,一个点击和一个点击并按住的手势。我的“点击并按住”手势是这样工作的:Ios 静态表视图单元格似乎具有相同的数据和选定的单元格索引?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,基本上,我需要两件事来处理我视图中的单元格,一个点击和一个点击并按住的手势。我的“点击并按住”手势是这样工作的: -(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer { NSLog(@"gestureRecognizer= %@",gestureRecognizer); if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
-(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];
}
}
我需要这个手势将singleton类中的值设置为特定值,具体取决于选择的行。无论选择哪一行,该值始终为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;
}
我的表视图中的每一行都有与第一行相同的信息?为什么会这样
谢谢,因为您使用的是可重用单元格,并且您已经在if()alloc init块中更改了textLabel 将
单元格.textLabel.text
行移出返回单元格正上方的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
仅在创建单元格时设置单元格值。“出列”可以返回已实例化的单元格。我打算用括号括起来
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;
...
你想要什么告诉我Simply你可以在创建单元格时设置手势