uitableview 行呈现奇怪的行为
我想要两个创建 2 个部分 uitableview 所以我做了下面的代码,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@" I entered numberOfSectionsInTableView");
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section = 0 ) {
1;
}
else {
9;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@" I entered cellForRowAtIndexPath");
NSLog(@"the Caf files count is : %d",[self.CafFileList count] );
NSLog(@" the txt file %d",[self.TXTFileList count] );
if (indexPath.section == 0 ) { // Enter here twice , even I say it contain only one row
NSLog(@"////////////////////////Section 0 %d" , [indexPath row] );
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:[self.TXTFileList objectAtIndex:[indexPath row] ] ];
return cell;
}
else if (indexPath.section == 0 && indexPath.row < [self.TXTFileList count] ){
NSLog(@"////////////////////////Section 1 %d" , [indexPath row] );
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:[self.CafFileList objectAtIndex:[indexPath row]] ];
return cell;
}
}
问题是它在这里输入两次,即使我说它只包含一行,
if (indexPath.section == 0 ) {
知道如何解决这个问题
I want two create 2 sections uitableview
so I did the following code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@" I entered numberOfSectionsInTableView");
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section = 0 ) {
1;
}
else {
9;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@" I entered cellForRowAtIndexPath");
NSLog(@"the Caf files count is : %d",[self.CafFileList count] );
NSLog(@" the txt file %d",[self.TXTFileList count] );
if (indexPath.section == 0 ) { // Enter here twice , even I say it contain only one row
NSLog(@"////////////////////////Section 0 %d" , [indexPath row] );
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:[self.TXTFileList objectAtIndex:[indexPath row] ] ];
return cell;
}
else if (indexPath.section == 0 && indexPath.row < [self.TXTFileList count] ){
NSLog(@"////////////////////////Section 1 %d" , [indexPath row] );
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:[self.CafFileList objectAtIndex:[indexPath row]] ];
return cell;
}
}
the problem is that it Enter here twice , even I say it contain only one row
if (indexPath.section == 0 ) {
any idea how to solve that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会在
numberOfRowsInSection
中返回行号,您只需输入一个数字,但不返回任何内容。另外,
设置
section
为 0,您希望使用==
运算符。You don't return row numbers in
numberOfRowsInSection
, you just enter a number, but return nothing.Also,
sets
section
to 0, you want to use the==
operator.还有你的代码:
你永远不会为部分== 1生成单元格。似乎会崩溃或错误地重用单元格
Also your code:
You never generate cell for section == 1. seem will crash or wrong reusing cell