2013-01-09 51 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