2014-03-27 24 views
0

我遇到了我的UITableviewCell問題,它只生成我的NSMutableArray中的第一個索引。我使用NSLog(@「Count%i%@」,[enterPrise_names count],enterPrise_names); 要檢查我的NSMutableArray裏面的物體數量,一切看起來都很好。 我已經連線了所有東西,包括UItableViewDataSource,UITableViewDelege和匹配TableViewCell的單元格標識符。帶有UITableViewCell的NSMutableArray

問題是我的兩個標籤僅顯示我的NSMutableArrays中的第一個對象。 我想發佈我的模擬照片給你們,但這是我的第一篇Stackoverflow文章,我沒有足夠的聲望來完成這個任務。

以及它看起來像這樣

富士暹

富士暹

富士暹

富士暹

富士暹

富士暹羅

這裏是我的代碼

#import "MyQueueViewController.h" 
#import "QueueCell.h" 
@interface MyQueueViewController() 

@end 

@implementation MyQueueViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 

    } 
    return self; 
} 
- (void)viewDidAppear:(BOOL)animated 
{ 


} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    enterPrise_names = [[NSMutableArray alloc]initWithObjects:@"Fuji",@"MK",@"KFC",@"PizzaHut",@"McDonal",@"BurgerKing", nil]; 
    BranchName = [[NSMutableArray alloc]initWithObjects:@"Siam",@"Paragon", @"CTW", @"Pinklao", @"Bangkae", @"Bangna", nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [enterPrise_names count]; 
} 

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

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"QueueCell"; 
    QueueCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[QueueCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 

    } 
    NSLog(@"Count %i %@", [enterPrise_names count], enterPrise_names); 
    cell.txtBranch.text = [BranchName objectAtIndex:indexPath.row]; 
    cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.row]; 

    return cell; 
} 

我想如果任何你們會指出我的錯誤是非常讚賞。 非常感謝您的幫助。

回答

1

你的tableview包含很多部分,但只有一行一行。在這裏根據您的陣列和行和段倒數

cell.txtBranch.text = [BranchName objectAtIndex:indexPath.section]; 
cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.section]; 

但是,如果你實際上並不需要很多的部分,你應該設定的行數:試試這個

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

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

Yeahhhhh it works !!! 非常感謝你我忽視了 非常感謝Justafinger,你是最棒的! – user3027109

+0

你知道我該如何解決這個問題?因爲我已經有了解決方案。我在這裏很新。 – user3027109

+1

你並沒有真正關閉問題,你只是接受答案。如果其他人有同樣的問題,他們將能夠看到什麼解決了它。 – streem

0

嘗試, 更改cellForRowAtIndexPath:方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    .... 
    NSLog(@"Count %i %@", [enterPrise_names count], enterPrise_names); 
    cell.txtBranch.text = [BranchName objectAtIndex:indexPath.section]; 
    cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.section]; 

    return cell; 
} 

由於每個部分具有一排,indexPath.row將在zero所有情況。因此BranchNameenterPrise_names陣列中只有第一個元素將顯示在標籤中。將indexPath.row更改爲indexPath.section將解決問題

+1

非常感謝您的幫助。 我忽略了這一點 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {enterPrise_names count]; (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { } return 1; } 如果我只交換這兩個返回值,那麼我的代碼將工作得很好。 ^^ 再次感謝您 – user3027109

+0

@ user3027109是的,您是對的。改變它將解決問題 – Akhilrajtr

相關問題