1
votes

I'm trying to subclass a UITableView. However, I'm unable to get an indexPath that isn't nil. The tableView has custom cells.

Here is my TouchTableView.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> //I brought this in because I'm saving audio files in my app
#import "SimpleTableCell.h"



@protocol myTableViewDelegate;

@interface TouchTableView : UITableView <UITableViewDelegate, UITableViewDataSource, AVAudioRecorderDelegate, AVAudioPlayerDelegate, UITextViewDelegate, UITextFieldDelegate, UIAlertViewDelegate>

@property (nonatomic, weak) id<myTableViewDelegate> myDelegate;
@property (strong, nonatomic) NSMutableArray *sortedFiles;
@property (strong, nonatomic) NSString *simpleTableIdentifier;
@property (strong, nonatomic) SimpleTableCell *cell;
@property BOOL inverted;

-(void)refreshTable;

@end


@protocol myTableViewDelegate <NSObject>

- (void)selectedFile:(TouchTableView *)tableView withURL: (NSURL *) tableViewURL IndexPath:(NSIndexPath *)indexPath;
-(void)didDelete:(TouchTableView *)tableView IndexPath:(NSIndexPath *)indexPath;
-(void)setSortedFile:(TouchTableView *)tableView;

@end

I attach a longpress like so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //bring in your custom cell here

    simpleTableIdentifier = @"SimpleTableCell";

    cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];

            [cell.textField setEnabled:NO];


            //put in long press
            UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
            [longPressGesture setMinimumPressDuration:1.0];

            [cell addGestureRecognizer:longPressGesture];

        }
    }
    return cell;
}

Then I have the following method for when the longpress activates:

- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
    if (![cell.textField isEnabled]) {
        // only when gesture was recognized, not when ended
        if (gesture.state == UIGestureRecognizerStateBegan)
        {
            // get affected cell
            cell = (SimpleTableCell *)[gesture view];

            // get indexPath of cell

            NSIndexPath *indexPath = [self indexPathForCell:cell];
            [self selectRowAtIndexPath:indexPath animated:NO scrollPosition:0];


            [cell.textField setEnabled:YES];
            [cell.textField becomeFirstResponder];


        }
    }
}

In my viewController in viewDidLoad I have:

self.touchTableView = [[TouchTableView alloc] init];

 [self.tableView setDelegate:self.touchTableView];
 [self.tableView setDataSource:self.touchTableView];

 self.touchTableView.myDelegate = self;

The problem is that the indexPath is always nil. You'll note that I'm calling self instead of self.tableView because self is the tableView. Is there a way to get the indexPath?

1
What you are doing makes no sense. Why does TouchTableView extend UITableView when it's not a table view. It's a controller should use a table view. But then your view controller seems to have its own table view and sets that table view's delegate and data source your TouchTableView instance. So now you have two table views, both with the same data source and delegate.rmaddy

1 Answers

0
votes

Sorry for the mess all. rmaddy was right. Ridiculous. The solution:

in my ViewController.h I set up:

@property (weak, nonatomic) IBOutlet TouchTableView *tableView;

Then I changed the last code in my example to

[self.tableView setDelegate:self.tableView];
[self.tableView setDataSource:self.tableView];  
self.tableView.myDelegate = self;
[self.tableView refreshTable];

Now I'm getting the index path.