13

I have a UIView which contains a UITableView. The tableview's delegate is set to my UIView, but it never calls the delegate methods:

-(id)init {
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds];
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled = NO;
        self.tableView.layer.cornerRadius = PanelCornerRadius;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - UITableViewDelegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"height for row");
    int height = [self heightForRowAtIndexPath:indexPath];

    return height;
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"number of rows");
    if (self.manager.fetchOperation.dataFetchProblem) {
        return 1;
    }

    int numberOfRows = [self.delegate numberOfSectionsInPanel:self];

    return numberOfRows;
}

I've explored every option I can think of, but can't seem to find the root of the problem.

EDIT: Included the numberOfRowsInSection. It could potentially return 0, only it never gets that chance because the "number of rows" NSLog never gets called.

Andrew
  • 15,935
  • 28
  • 121
  • 203
  • 1
    What are you returning from `numberOfRowsInSection:`? – Lance Sep 23 '13 at 22:38
  • Also, if this is in a UIView like you said, you should put the code that's in `init` to `awakeFromNib` – Lance Sep 23 '13 at 22:39
  • @Lance I have an NSLog in there too - it doesn't get called. I edited into my post above. – Andrew Sep 23 '13 at 22:40
  • 3
    Do you ever call "reloadData"? – Rok Jarc Sep 23 '13 at 22:40
  • 1
    put a call to `reloadData` in `willMoveToSuperview` – Lance Sep 23 '13 at 22:42
  • I subclassed the UITableView and put in a setReloadData:. It doesn't get called. – Andrew Sep 23 '13 at 22:44
  • @Lance just tried that. Nothing. I even tried checking for the delegate at that point. Delegate is still set to my view. – Andrew Sep 23 '13 at 22:47
  • You should also implement `- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView` and return at least 1. – gWiz Sep 23 '13 at 23:16
  • 2
    Would it be possible that you didn't add the UIView into another visible view? The tableview will only be drawn when it is visible. Otherwise I can't see anything wrong from your code. I do the same thing before, and it works as charm. – Vincent Sep 24 '13 at 00:58
  • Are you adding this view that holds tableView from code of within Interface Builder? – Rok Jarc Sep 24 '13 at 07:09

8 Answers8

22

Can you please write the code that you have written on your cellForRowAtIndexPath: method? Because i can't find anything wrong with your code.

Are you calling your UIView from some other ViewController? If yes, how?

I have done something like this once. I declared a method in the UIView like this :

- (void)setUpTableView{
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
}

And then i called the setUpTableView from the view controller i added this view to, like this :

[self.yourView setUpTableView]; 

This might help. Thanks.

16

I had a similar problem, my solution was because I did not set the number of sections to 1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Darkin
  • 221
  • 2
  • 4
3

You should declare your view like this @interface MyView : UIView <UITableViewDelegate, UITableViewDataSource>

BTW: I would have a ViewController that "controls" your view and your tableview, and have the ViewController be the delegate and datasource of the table view.

gWiz
  • 1,274
  • 9
  • 12
  • 1
    I don't think the *.h declaration is the issue. However, I totally agree with having a view controller that controls the view and the table view. That's what they're there for. +1 – LuisCien Sep 23 '13 at 22:54
  • Ah yea, this makes sense. I'm sure the tableview calls `conformsToProtocol:` on the delegate/dataSource – Lance Sep 23 '13 at 23:00
  • @LuisCien You are right. the .h isn't the issue. I just checked that in one of my projects. I removed these protocols and the methods do get called. – gWiz Sep 23 '13 at 23:01
3

You do not need to use a UIViewController, that's just a smokescreen. You also do not need to add to the .h, although you should do that anyway because Xcode will complain otherwise, and you won't get method name auto complete.

I just wrote a test project that embeds a table view in a normal UIView and it works fine. Just make sure your initialization code is being called. I bet you need to move it to awakeFromNib.

jsd
  • 7,673
  • 5
  • 27
  • 47
1

I ran into the same issue once, what silly mistake I did was inside initWithCoder I called [super init]. TableView was in xib

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super init]
}

Instead of

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
}

Just check if that's not the case

user3894518
  • 677
  • 5
  • 10
1

try this:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

i had this stupid problem too

ironRoei
  • 2,049
  • 24
  • 45
0

Try changing your UIViewto UIViewController and in viewDidLoad, call

[[UITableView alloc] initWithFrame:style:] to create your Table View.

Set self as the delegate and data source like you did above, and then add this UITableView as Subview in this Controller.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
0

I accidentally set my tableView's allowsSelection property to false.

Storyboard solution

Select your table view and set the following... enter image description here

Swift solution

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.allowsSelection = true
}

Notes

  1. At first, I thought this was a UITableViewDelegate issue (as other answers suggested). It wasn't. I linked it to my view controller in my storyboard.
  2. Next, I thought it was a UITableViewDataSource issue, in that I didn't implement the protocol's numberOfSections(in tableView:) method (as other answers suggested). It wasn't. According UIKit's documentation,

// Default is 1 if not implemented

  1. Finally, I checked the settings on my table view :]
Andrew Kirna
  • 1,346
  • 16
  • 18