简体   繁体   中英

UITableView DataSource method called twice

My topic name might be seems familiar and it is.but believe me i've tried to get some help from other answers on stack overflow but still i can not understand, why DataSource method loads twice while i'm initialising tableview only once.

(Xcode6.3)

see my code...

#pragma mark- UITableView -
#pragma mark Setup TableView
-(void)setupTableView{
    if(!tblEvent){
        CGRect frame=CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y+55+50,self.view.frame.size.width,self.view.frame.size.height-(55+100));
        tblEvent = [self createTableViewWith_Frame:frame Tag:1 separatorColor:[UIColor blackColor] BackgroundColor:[UIColor clearColor] ScrollIndicators:false];
        tblEvent.delegate=self;
        tblEvent.dataSource=self;
        [self.view addSubview:tblEvent];
    }else{
        [tblEvent reloadData];
    }
}
#pragma mark Create
-(UITableView*)createTableViewWith_Frame:(CGRect)frame Tag:(NSUInteger)tag
                          separatorColor:(UIColor*)separatorColor
                         BackgroundColor:(UIColor*)bgColor ScrollIndicators:(BOOL)value
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
    tableView.tag=tag;
    tableView.backgroundColor=bgColor;
    tableView.separatorColor= separatorColor;
    tableView.showsVerticalScrollIndicator = value;
    tableView.showsHorizontalScrollIndicator= value;

    tableView.scrollEnabled = YES;
    tableView.separatorInset=UIEdgeInsetsMake(0, 8, 0, 8);
    tableView.userInteractionEnabled = YES;
    tableView.bounces = YES;

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifire];

    return tableView;
}

#pragma mark UITableView DATASOURCEs
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//this method called 3 times, i don't know why.? :(
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return totalEvents.count;//this is my source.
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifire forIndexPath:indexPath];
    if(!aCell){
        aCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
    }

    return aCell;
}

#pragma mark UITableView DELEGATEs
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 127.0f;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView* view = [[UIView alloc]init];
    return view;
}

i don't use didSelectRow:AtIndexPath: because i don't need to row selection.

NOTE:-

i use setupTableView method in Successful response of web service and web service is called from viewDidLoad . please guide me.

Is there anything in your view controller that could be triggering a layout event (like a change to the table header view or some such)? That would cause it to call the datasource methods more than one.

PS SO won't allow me to post a comment to your question...

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