2013-01-09 53 views
1

我有2個視圖-view1和view2,一個用於輸入數據,另一個用於顯示輸入的數據。 我可以在標籤中的視圖2中顯示輸入的數據。但是如何在UITableView中顯示數據。以下是我的代碼來顯示數據:在UITableView中顯示導航數據

view2.m

@synthesize details; //details is an object of NSMutableArray. 
    - (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    textLabel.text = self.name; 
    lblCity.text = self.city; 
    lblGender.text = self.gender; 


} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [details count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.text = [details objectAtIndex:indexPath.row]; 
    //cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"]; 
    return cell; 

我debuuged,發現cellForRowAtIndexPath永遠不會被調用。

我在哪裏出錯?我如何解決它?

+1

難道ÿ你可以將tableView的'dataSource'和'delegate'設置爲你的對象嗎? –

+0

對不起,但我是新來的ios,我如何設置它們? – z22

+0

如果你在xib中使用了表,然後將它的委託方法設置爲文件所有者。看到這個http://www.techotopia.com/index.php/Creating_a_Simple_iOS_4_iPhone_Table_View_Application_%28Xcode_4%29 – Dhara

回答

2

您需要到UITableViewCell像下面創建顯示數據的NSMutableArray的,不要忘了在.H文件<UITableViewDataSource,UITableViewDelegate>申報的UITableView代表和也連接您的UITableView IBOutlet中在XIB,也連接委託:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    arrCategorisation=[[NSMutableArray alloc] initWithObjects:@"DataOne",@"DataTwo",@"DataThree", @"DataFour",@"DataFive",nil]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrCategorisation.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier =[NSString stringWithFormat:@"%d",indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      cell.textLabel.text=[arrCategorisation objectAtIndex:indexPath.row]; 

    } 

    // Configure the cell... 

    return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

您的表加載數據,如: -

enter image description here