【发布时间】:2015-06-04 05:27:33
【问题描述】:
我的主题名称可能看起来很熟悉,但相信我,我已经尝试从堆栈溢出的其他答案中获得一些帮助,但我仍然无法理解,为什么 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:,因为我不需要行选择。
注意:-
我在Web服务的成功响应中使用setupTableView方法,并且从viewDidLoad调用Web服务。请指导我。
【问题讨论】:
-
这似乎是正常的。如果它运行不止一次有什么问题?顺便说一句,
dequeueReusableCellWithIdentifier:forIndexPath:保证返回一个单元格,因此无需检查零单元格。此外,如果您有一个部分,则无需实现numberOfSectionsInTableView,因为这是默认设置。 -
您的
UITableView中有.xib或Storyboard吗? -
感谢@rdelmar 的评论。让我根据你的评论试试。
-
@gran33,如您所见,我正在 Storyboard 中以编程方式创建 tableview。 :D
-
糟糕,没错:)
标签: ios objective-c uitableview