简体   繁体   中英

Show rownumber in Alertview when cell being Swiped to the left. Swipe Gesture added in interface builder

I want to display the row number of the cell being swiped in a alertview. The problem i have is that i can't get the row number that's being swiped, it shows "0" all the time.

I've added the swipe gesture with the interface builder.

My .h file

@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *gestureSwipeLeft;
@property (nonatomic, retain) NSIndexPath * indexPath;
@property (nonatomic, assign) int test;

My .m file

- (IBAction)handleSwipeLeft:(UISwipeGestureRecognizer *)sender{

UIAlertView *getRow = [[UIAlertView alloc]initWithTitle:@"RowNumberSwiped" message:[NSString stringWithFormat:@"%i", _test] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[getRow show];
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
_test = self.indexPath.row;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myBasicCell"];

thingToDoDoc *things = [self.things objectAtIndex:indexPath.row];

UILabel *lblTitle = (UILabel *)[cell viewWithTag:100];
lblTitle.text = things.data.title;

UIImageView *ratingImageView = (UIImageView *)[cell viewWithTag:101];
//  _lblTitle.text = things.data.title;
if (things.data.isDone == 2) {
    ratingImageView.image = [UIImage imageNamed:@"checkmark.png"];
}

return cell;
}

You don't require _test int variable for what you are trying to achieve. And as per your current code in tableView:cellForRowAtIndexPath, you are assigning new indexPath.row value to _test every time this method is called(_test is overwritten every time). And even you are not setting the value of self.indexPath anywhere in the mentioned code. You will have to get the currently swiped row's index to show in Alertview in a different way. See (and replace the method with) following code:

- (IBAction)handleSwipeLeft:(UISwipeGestureRecognizer *)sender{

    UIAlertView *getRow = [[UIAlertView alloc]initWithTitle:@"RowNumberSwiped" message:[NSString stringWithFormat:@"%i", [yourTableView indexPathForRowAtPoint:[self.gestureSwipeLeft locationInView:yourTableView]].row] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [getRow show];
}

Hope this helps you out.

As mentioned in my comment above _test is being overwritten every time cellForRowAtIndexPath is called, try something like this inside you gestureRecognizer callback

-(void)cellSwipe:(UISwipeGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:tableView];
    NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
    UITableViewCell *swipedCell  = [tableView cellForRowAtIndexPath:swipedIndexPath];

    //Your own code...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM