Ios UITableView数据源方法调用两次
我的主题名可能看起来很熟悉,但请相信我,我曾试图从其他关于堆栈溢出的答案中获得一些帮助,但我仍然不明白,为什么DataSource方法加载两次,而我只初始化tableview一次 (Xcode6.3) 查看我的代码…Ios UITableView数据源方法调用两次,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的主题名可能看起来很熟悉,但请相信我,我曾试图从其他关于堆栈溢出的答案中获得一些帮助,但我仍然不明白,为什么DataSource方法加载两次,而我只初始化tableview一次 (Xcode6.3) 查看我的代码… #pragma mark- UITableView - #pragma mark Setup TableView -(void)setupTableView{ if(!tblEvent){ CGRect frame=CGRectMake(self.view.
#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;
}
我不使用didSelectRow:AtIndexPath:
,因为我不需要进行行选择。
注意:-
我在web服务的成功响应中使用了
setupTableView
方法,并从viewDidLoad
调用web服务。请告诉我。您的视图控制器中是否有任何东西可能触发布局事件(如更改表头视图或类似事件)?这将导致它调用多个数据源方法
另外,我不允许我对你的问题发表评论…这似乎很正常。如果它运行多次,会有什么问题?顺便说一句,
dequeueReusableCellWithIdentifier:forIndexPath:
保证返回一个单元格,因此不需要检查nil单元格。此外,如果您有一个节,则无需实现numberOfSectionsInTableView
,因为这是默认设置。您的.xib
或故事板中是否有UITableView
?感谢@rdelmar的评论。让我根据你的评论试试。@gran33,正如你所见,我正在故事板中以编程方式创建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;
}