2010-08-15 52 views
2

與我的NSMutableArray和UITableView有一個奇怪的問題。UITableView複製所有行的第一個NSArray項目

它有數組中的4項,該表顯示4行,但每行只包含第一陣列的項目,而不是第1行顯示陣列第1項,第2行顯示2項,等等...

這裏是我的代碼:

我可變數組 //在.H

NSMutableArray *tableRows; 

//在.M

tableRows = [[NSMutableArray arrayWithObjects:@"For Business, For Pleasure", 
        @"A Great Decision", 
        @"Air Charter Your Honeymoon", 
        @"Time Flies", nil] retain]; 

我的表中的數據源和委託具有

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
    return (NSInteger)[tableRows count]; 
} 
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"CELL_AIR"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if(cell == nil) { 
     cell = [self makeTableCell:cellID identifier:indexPath]; 
    } 
    return cell; 
} 

-(UITableViewCell *)makeTableCell:(NSString *)identifier identifier:(NSIndexPath *)indexPath { 
    //frames 
    CGRect cellFrame = CGRectMake(0, 10, 320, 50); 
    CGRect lbl1Frame = CGRectMake(10, 0, 320, 25); 
    CGRect lbl2Frame = CGRectMake(10, 20, 320, 20); 


    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:identifier]; 

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:lbl1Frame]; 
    lbl1.tag=1; 
    lbl1.font = [UIFont systemFontOfSize:19]; 
    lbl1.text = [tableRows objectAtIndex:indexPath.row]; 


    UILabel *lbl2 = [[UILabel alloc] initWithFrame:lbl2Frame]; 
    lbl2.tag=2; 
    lbl2.text = @"Tap to read more..."; 
    lbl2.font = [UIFont systemFontOfSize:12]; 

    [cell.contentView addSubview:lbl1]; 
    [cell.contentView addSubview:lbl2]; 

    return cell; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    return 1; 
} 

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 50; 
} 

任何幫助將是巨大的感謝!

感謝

Ç

回答

3

這將導致錯誤:

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    return 1; 
} 

你只有1每個部分行的。因此,對於每一個部分,該指數路徑從0開始又那麼你只得到第一個項目在陣列

我覺得做的最好的方法應該是:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    return (NSInteger)[tableRows count]; 
} 
+0

非常好。謝謝 - 將標記爲已回答。 – 2010-08-15 12:40:38

1

添加上已經回答了什麼vodkhang,這是我該怎麼做:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
return [tableRows count]; 
} 

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *cellID = @"CELL_AIR"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
if(cell == nil) { 
    cell = [self makeTableCell:cellID identifier:indexPath]; 
} 

[[cell textLabel] 
     setText:[tableRows objectAtIndex:indexPath.row]]; 

return cell; 
} 

我真的不使用(NSInteger)[數組數]。由於方法計數返回整數類型。我希望這有幫助。

+0

這確實有幫助,謝謝梅爾文。 :) – 2010-08-18 11:26:46

相關問題