我有一个包含 6 个单元格和 6 个分隔符的静态表格视图。我需要单元格 1 的索引为 0,单元格 6 的索引为 5,这可能吗?我下面的代码不起作用,因为每个单元格都在一个单独的部分中,所以它认为每次都选择了单元格 0,并且它再次使用相同的数据填充单元格,它认为它的单元格 0。
-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer
{
NSLog(@"gestureRecognizer= %@",gestureRecognizer);
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
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];
}
}
- (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;
return cell;
}
我刚刚尝试过这个来获取标签:
CGPoint p = [gestureRecognizer locationInView:myTable];
NSIndexPath *indexPath = [myTable indexPathForRowAtPoint:p];
UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
NSLog(@"TAG IS : %i", cell.tag);
但是,每个单元格仍然用我表中第一个单元格的值标记?