简体   繁体   中英

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.

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;

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

确保已通过代码或通过Interface Builder出口将表视图连接到其委托。

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.

如果设置了所有委托,则所有问题可能是UITableview的大小小于单元格的大小。

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM