简体   繁体   中英

UITableView delegate methods not called from UIView

I am using a UIView as a popup in my app.

I have added a UITableview in to this UIVIew . However, the content of the UITableView doesn't get populated. I have set the delegate and datasource from IB and also from the code as well.

None of the Delegate methods are being called.

Code as follows:

-(id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

self.tablev.delegate = self;

        self. tablev.dataSource = self;


        [self addSubview:self. tablev];

}

View .h class

@interface MyView : UIView <UITableViewDataSource,UITableViewDelegate>

UPDATE

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

    MyView

 *cell = [tableView dequeueReusableCellWithIdentifier:@"c" forIndexPath:indexPath];

    if (cell == nil)

    {

        cell = [[MyView

 alloc] initWithStyle:UITableViewCellStyleSubtitle

                                             reuseIdentifier:@"c"];

    }

    cell.la.text=@"hiiiii";

1) If you have created the UIView popup and Table View using Interface Builder, then just set the delegate and dataSource of the table view in xib and also in this case you need not to add the tableview to the view (like [self addSubview:tableView] ), because it's already added via on he view while creating it in xib.

2) If you need to set any thing via code on the views which are created using xib than use

- (void) awakeFromNib { } method of the corresponding view

awakeFromNib is called after the xib is loaded and it's subviews could be populated/customized.

3) Implement the delegate and dataSource methods

I think -

1) Your view should be MyView created using xib

2) Add a table view on this view (in xib), and set delegate and data source

3) Crete a class for CustomCell (using which cells will be created)

4) Implement the delegate and dataSource methods in MyView class

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c" forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c"];
    }

    cell.la.text=@"hiiiii";

    return cell;
}

I was doing something similar and faced the same problem. UITableViewDataSource methods were not being called in my case. In my case this happened because the array used for populating the table was weak , I made it strong and problem was solved for me. Hope this will also help you.

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