0

I have a UITableView that I am using to show data that requiers me to have a much wider cell than the iPhone screenWidth.

Because of this I have created a subclassed UITableViewCell that contains a subclassed UIScrollView. This is what my code looks like.

TableView.h

@property (strong, nonatomic) CustomCell *cell;

TableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cellDictionary = [xmlMArray objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell.
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    cell.itemsDictionary = cellDictionary;
    cell.currentIndex = indexPath;

    cellDictionary = nil;
    [cell drawCell];

    return cell;
}

In the above method you can see I am calling a custom UITableViewCell that I have created.

CustomCell.h

@property (strong, nonatomic) NSDictionary *itemsDictionary;
@property (strong, nonatomic) MyScrollView *scrollCell;
@property (strong, nonatomic) NSIndexPath *currentIndex;

- (void)drawCell;

CustomCell.m

- (void)drawCell
{
    scrollCell = [[MyScrollView alloc] init];
    scrollCell.itemsDictionary = itemsDictionary;
    [scrollCell drawCell];
    scrollCell.backgroundColor = [UIColor whiteColor];
    scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);

    [[self contentView] addSubview:scrollCell];
}

In the above drawCell method I am calling a custom scrollView

MyScrollView.m

- (void)drawCell
{
    bgGray = NO;
    fNameString = [[UILabel alloc] init];
    fNameString.backgroundColor = [UIColor clearColor];
    fNameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
    fNameString.text = [itemsDictionary objectForKey:@"fName"];

    lNameString = [[UILabel alloc] init];
    lNameString.backgroundColor = [UIColor clearColor];
    lNameString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
    lNameString.text = [itemsDictionary objectForKey:@"lName"];

    addressString = [[UILabel alloc] init];
    addressString.backgroundColor = [UIColor clearColor];
    addressString.frame = CGRectMake(220.0, 10.5, addressString.frame.size.width, 50.0);
    addressString.text = [NSString stringWithFormat:@"Address: %@: %@",[itemsDictionary objectForKey:@"aNumber"] ,[itemsDictionary objectForKey:@"aString"]];
    [addressString sizeToFit];

    [self setContentSize:(CGSizeMake((220.0 + addressString.frame.size.width)+15, 45.0))];

    [self addSubview:fNameString];
    [self addSubview:lNameString];
    [self addSubview:addressString];
}

The above method draws the scrollview that is then passed onto the CustomCell, everything works fine and displays correctly, below is the method I am using to detect a touch on the cusome scroll view its in the same class as the method above and looks like this.

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
    NSLog(@"touch scroll");
    // If not dragging, send event to next responder
    if (!self.dragging) {
        // touch detected...
        //How can I now call didSelectRow from TableView?
    } else {
        [super touchesEnded: touches withEvent: event];
    }
}

So my question is this, how can I use this touchEnded method to call didSelectRowFromIndexPath method that is in the original TableView class? Or is there a better way to do this than what I am currently doing?

Note: the reason I have had to use these subclasses is because the UIScrollView is covering the UITableViewCell selection function, so I had to subclass the scrollview to intercept the touch event now I'm hoping I can call the original tableview selection method with your help.

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

1 Answers1

5

How about calling the delegate function using the following code:

[yourTableView.delegate tableView:tableView didSelectRowAtIndexPath:aIndexPath];

In your subclassed cell store a weak reference to your tableview and indexPath.

Another solution is to loop through your cell's superview until you find the tableview. This has its cons though, Apple may one day change the way the view hierarchy structured which will break the code as they did from iOS6 to iOS7 by wrapping another layer around some views...

Shams Ahmed
  • 4,498
  • 4
  • 21
  • 27
  • I am passing the indexpath of each cell to itself... **cell.currentIndex = indexPath;** so if I can somehow pass the current tableView to the subclassed cell and scrollview maybe I can call the didselectrowatindexpath like that? – HurkNburkS Mar 24 '14 at 23:49
  • 2
    if you store your cell's tableView as a property in the cell as you do for the index path then you should be able to invoke the delegate method. – Paulw11 Mar 24 '14 at 23:53
  • 1
    As @Paulw11 said store it in a property with weak reference to avoid circular dependency or another way is to look up the superview of the cell see this [answer](http://stackoverflow.com/questions/15711645/how-to-get-uitableview-from-uitableviewcell) for code snippet – Shams Ahmed Mar 24 '14 at 23:59
  • @Shams. If you can edit your question to add this I can remove my down vote which was placed based on your original solution which would have crashed – Paulw11 Mar 25 '14 at 00:00
  • Yep it worked! by passing a refrence to the tableview all the way to the subclassed scrollview I am able to call didSelectRowAtIndexPath... feels kind of a hack way of doing it but its the best way I have found so far! thanks fo rthe help. – HurkNburkS Mar 25 '14 at 00:17
  • @ShamsAhmed that superview idea is good.. I forget how to go higher than one view.. might try and see if that will work also. – HurkNburkS Mar 25 '14 at 00:22
  • So, I tried using this when extending a framework class, CABTMIDICentralViewController. It's crashing saying there's no selector. Is that something Apple hides so you can't extend their objects? – phreakhead Oct 29 '15 at 21:46
  • not really it should be fine, not used CoreAudioKit so i cant say for sure, usally state on doc – Shams Ahmed Oct 30 '15 at 00:14