2012-02-22 72 views
0

我想要两个创建2个部分的UITableView 所以我做了以下代码的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) { 

任何想法如何解决

+2

如果(节= 0){?然后1; :-) – 2012-02-22 15:08:37

+0

抱歉可以解释更多 – AMH 2012-02-22 15:14:02

回答

4

你不numberOfRowsInSection回行号,你只要输入一个数字,而是返回任何结果。

此外,

if (section = 0) { 

section为0,你要使用的==操作。

+0

你是什么意思?如果(section = 0){ – AMH 2012-02-22 15:13:07

+1

你的'numberOfRowsInSection:'函数做到了这一点。 =运算符是一个赋值运算符,您应该将其更改为==,它是比较运算符 – JiaYow 2012-02-22 15:15:29

+0

它的工作原理,但它没有返回任何部分 – AMH 2012-02-22 15:18:28

3
if (section == 0) { 
    return 1; 
} 
else { 
    return 9; 
} 

而且代码:

else if (indexPath.section == 0 && indexPath.row < [self.TXTFileList count] ){ 

你永远不会产生部分== 1细胞似乎会崩溃或错误的再利用细胞