两次调用的UITableView DataSource方法

时间:2015-06-04 05:27:33

标签: ios objective-c uitableview

我的主题名称可能看起来很熟悉而且确实如此。但是我相信我已经尝试从堆栈溢出的其他答案中获得一些帮助但仍然无法理解,为什么DataSource方法在我初始化tableview时加载两次一次。

  

(Xcode6.3)

查看我的代码......

#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;
}

我不使用didSelectRow:AtIndexPath:,因为我不需要行选择。

注意: -

我使用setupTableView方法成功响应Web服务,并从viewDidLoad调用Web服务。请指导我。

1 个答案:

答案 0 :(得分:0)

视图控制器中是否有任何可能触发布局事件的内容(如更改表头视图或某些内容)?这将导致它不止一次调用数据源方法。

P.S。因此,我不允许对您的问题发表评论......

相关问题