0

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?

Community
  • 1
  • 1
RNelke
  • 87
  • 7
  • Refer this: http://stackoverflow.com/questions/5375122/how-to-select-multiple-rows-in-uitableview – Mrunal Mar 15 '13 at 16:12
  • @mrunal The poster has a specific problem with his current approach, totally re-doing it with another approach is overkill. I think this is a one-line fix. –  Mar 15 '13 at 16:14

2 Answers2

1

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).

  • That removed the error message, thank you! I allready thought I had to change something in the framework of `UITableView.h` And thank you for the explanation. If that doesn't lead to far and just for my understanding: With `self` I reference to the `MyTableViewController` and with `tableView` I reference to the superclass `UITableView`, right? – RNelke Mar 15 '13 at 16:22
  • You are correct: `self` is an instance of `MyTableViewController`, which is a subclass of `UITableViewController`. `tableView` is an instance of `UITableView`. (Actually I *guessed* that your `indexPathIsSelected ` method was implemented on `MyTableViewController` but it looks like a correct guess; that's the natural place to implement that.) –  Mar 15 '13 at 16:26
  • In the mean time I ran my code and it worked finally. Not only it works but I also understand my mistake thanks to your explanation. So thank you very much! – RNelke Mar 15 '13 at 16:38
0

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.

Blake Schwendiman
  • 442
  • 1
  • 4
  • 9
  • Just double checked it and the custom class is correctly set to `MyTableViewController`. It worked before, I just had the problem mentioned in the linked question. But thank you for your quick and friendly answer! – RNelke Mar 15 '13 at 16:10