开发者

iOS: Custom UITableViewCell touch receiver confusion

开发者 https://www.devze.com 2023-03-01 13:24 出处:网络
have seen similar questions but couldn\'t find a definitive answer. Having mastered regular tables of most types, I am doing some conceptual experiments with custom table cells to get familiar with h

have seen similar questions but couldn't find a definitive answer.

Having mastered regular tables of most types, I am doing some conceptual experiments with custom table cells to get familiar with how this works. I want to have a custom subclass of UITableViewCell which loads a nib to the contentView. I may want to implement different editing styles at some later point but want to reuse the custom view in different parts of my application, however, i'm having problem receiving the didSelectRowAtIndexPath message in my calling UITableViewController.

Here's the hierarchy which is built from a basic view template.

CustomCellViewController: A stock XCode objective-c class sublcassed from UITableViewCell

@interface CustomCellViewController : UITableViewCell {
  IBOutlet UILabel *lbl;
}

@property (nonatomic, retain) IBOutlet UILabel *lbl;

@end

@implementation CustomCellViewController

@synthesize lbl;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        NSArray *a = [[NSBundle mainBundle] loadNibNamed:@"customCellView" owner:self options:nil];
        UIT开发者_StackOverflow社区ableViewCell *tc = [a objectAtIndex:0];
        NSLog(@"Cell loaded from nib");
        [self.contentView addSubview:tc];
    }
    return self;
}

.. the other stock methods are unchanged ..

@end

I realise that the init method could be simplified but this is my first attempt.

The XIB file's owner is my custom class (CustomCellViewController), has a UITableViewCell and a label (linked to the outlet 'lbl' on it) positioned half way accross, leaving plenty of the underlying UITableViewCell clickable.

RootViewController is a standard, stock XCode subclass of UITableViewController RootViewController sets up an instance variable "CustomTableCellController *myCustomCell"

The cellForRowAtIndexPath: is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myCustomCell";

    CustomCellViewController *cell = (CustomCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {  
        cell = [[[CustomCellViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        NSLog(@"Creating New cell for position %d", indexPath.row);
    } else {
        NSLog(@"Reusing cell for position %d", indexPath.row);
    }

    // Configure the cell.
    cell.lbl.text = [NSString stringWithFormat: @"Hi There %d", indexPath.row];
    return cell;
}

And in the same RootViewController.m, my didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Cell tapped at row %d", indexPath.row);
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}

Designed at present, to purely output a log message when tapped.

numberOfSectionsInTableView returns 1 numberOfRowsInSection returns 50

This all compiles and runs fine, iPhone simulator starts, I see a table on the display, my output log confirms it has created 9 versions of CustomCellViewController and I can see the reuse stack via the NSLog() comments.

I just can't select a row, doesn't matter where I click in the custom cell, nothing gets to my didSelectRowAtIndexPath: in my RootViewController which is where I expect it.

Is it that I have I not set a delegate somewhere and if so, how? Do I need to do this via a first responder? (ie, create a method in my CustomCellViewController class, link the UITableViewCell from the XIB to that method and then call [super didSelectRowAtIndexPath] - but how do I pass the indexPath?

Am I not responding to a message from my XIB in it's owner and then passing it on (is this how I do it?)

I read through all the apple docs to get to this stage but couldn't quite decipher how touch messaging happened.

Slightly confused!


May be you have forget to set data source and delegate if the tableview object set it as like below

tbl.delegate = self;
tbl.dataSource = self;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号