2012-02-22 70 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細胞似乎會崩潰或錯誤的再利用細胞