Gaojian922188
Gaojian922188

Reputation: 503

tableview delegate method never called

I have a viewcontroller, and implement the delegate UITableViewDelegate and UITableViewDataSource, so I have following method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [_nameArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"name";
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
   static NSString *TableIdentifier = @"TableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier]autorelease];
    }
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = UITextAlignmentLeft;    
    nameLabel.text = @"100";
    nameLabel.backgroundColor = [UIColor blackColor];
    nameLabel.textColor = [UIColor yellowColor];
    [cell.contentView addSubview:nameLabel];
    [nameLabel release];
    return cell;
}

I set a breakpoint for each method, I can find

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

and

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

never called,but the other two methods can be called. so what's the problem? thank you.

update:

I have connect datasoucre and delegate to file's owner in IB,but can't solve the problem.

Upvotes: 0

Views: 6447

Answers (6)

Nitesh
Nitesh

Reputation: 2024

I have faced similar problem. datasource was working but delegate wasn't. If you have added "UITapGestureRecognizer" to "self.view", that is creating problem here. in this case table delegate related to user interaction will not be called. because delegates such as "didSelect" call when any user interaction happen. in this case if there is UITapGestureRecognizer on view , table delegate will not work.

Upvotes: 2

yashwanth
yashwanth

Reputation: 1

If you have set all the delegates and all then the problem might be the size of UITableview less than the size of cell.

Upvotes: 0

nik
nik

Reputation: 2329

Most likely its because of delegate reference problem .. make sure you have given proper reference to delegate methods..

In code use these two lines

self.tableView.delegate = self;
self.tableView.dataSource = self;

Upvotes: 12

UPT
UPT

Reputation: 1480

_nameArray have the 0 objects that's why it's not coming cellForRowAtIndexPath, because if _nameArray is nil or 0 objects delegate method numberOfRowsInSection returns 0 and it's not coming in both of the above mentioned methods.

For quick check you can return harcoded value say 10 from method numberOfRowsInSection and then see, it will call the cellForRowAtIndexPath method.

Upvotes: 5

arclight
arclight

Reputation: 5310

Have you set the delegate of the tableView to your class?

Also, unless you're subclassing UITableViewController you should also implement

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

Upvotes: 0

BoltClock
BoltClock

Reputation: 723388

Make sure you have connected the table view to its delegate, either in code or via Interface Builder outlet.

Upvotes: 3

Related Questions