RNelke
RNelke

Reputation: 87

Objective-C: How to declare/define method used on tableView

My initial problem of multiple line selection in a UITableView has been answered in this question. But the answer left me at a point where I can't go on on my own, as I am very new to Objective C and iOS development.

Following daxnitros answer, I want to implement the code he/she suggested:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView indexPathIsSelected:indexPath]) {
        [tableView removeIndexPathFromSelection:indexPath];
    } else {
        [tableView addIndexPathToSelection:indexPath];
    }
    // Update the cell's appearance somewhere here
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

I still need the methods and I thought I can do it for indexPathIsSelected (for example) like this:

@interface MyTableViewController ()
- (BOOL)indexPathIsSelected:(NSIndexPath *)indexPath;
@end

@implementation MyTableViewController

// ...

- (BOOL)indexPathIsSelected:(NSIndexPath *)indexPath
{
    BOOL bIsSelected = NO;
    // ...
    return bIsSelected;
}
@end

But that doesn't work. The error message is: No visible @interface for 'UITableView' declares the selector 'indexPathIsSelected:' Note: The same happens, if I declare the method in the .h file's interface instead.

Now, what baffles me, is this: [tableView indexPathIsSelected:indexPath] is somehow called on the tableView object and I don't even know why. Is that something I have to take into account in my method declaration/definition? I feel really stupid right now, that I can't even write a method by seeing its invocation.

How do I define and declare the method indexPathIsSelected correctly, so I can use it properly?

Upvotes: 0

Views: 460

Answers (2)

user2088639
user2088639

Reputation:

In your didSelectRowAtIndexPath, the variable tableView is a UITableView.

Your implementation for indexPathIsSelected is in class MyTableViewController, which is probably a UITableViewController.

UITableViewController and UITableView are different classes.

So you can't find the method indexPathIsSelected on UITableView because it's not implemented there, it's implemented on MyTableViewController which is a different class.

SO... I'm going to take an educated guess and assume that didSelectRowAtIndexPath is part of class MyTableViewController. If this is the case, then

[self indexPathIsSelected:indexPath]

may be the answer (i.e. call indexPathIsSelected in self rather than the table view).

Upvotes: 1

Blake Schwendiman
Blake Schwendiman

Reputation: 452

The error message you're seeing is the key to the problem. The method indexPathIsSelected is implemented in your custom class MyTableViewController. However, the UITableView you have is apparently still a basic UITableView. At the very least you'll need to go into the storyboard and set the custom class of the table view controller object to MyTableViewController.

To do this, open the storyboard (or nib) and select the table view controller. Then in the identity inspector (on the right hand side, typically), under custom class, select MyTableViewController from the drop down.

Upvotes: 0

Related Questions