2012-06-02 250 views
4

我是新來的X代碼,剛開始工作我的第一個應用程序。我正在使用故事板。在導航控制器場景中,我添加了一個帶有兩個單元的MasterViewController,這導致了兩個DetailViewControllers(Detail1 & Detail 2)。在= 2 在XCode中添加單元格到表格視圖(UITableView)

我添加了一個複用指示器用於在屬性檢查員的細胞,並取得在故事板中的連接柱

  • 列數= 1
  • 的行數。

    但是當我運行該應用程序時,我只能看到第一個單元格x2。

    當我查看MasterViewController.m時,我可以看到第一個單元的代碼,但我不明白如何插入第二個單元格等!

    我知道解決的辦法是容易的,我一直在尋找的答案爲3天,我覺得自己很蠢,但我真的需要一些幫助,現在,,

    有人可以幫助我瞭解如何添加更多的細胞在代碼中,也許可以更多地理解表視圖代碼?

    這是表視圖到目前爲止的代碼:

    #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 the number of rows in the section. 
    return 2; 
    } 
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"Formal"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    
    // Configure the cell... 
    
    return cell; 
    } 
    
    /* 
    // Override to support conditional editing of the table view. 
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
    } 
    */ 
    
    /* 
    // Override to support editing the table view. 
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
        // Delete the row from the data source 
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath][withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
    } 
    */ 
    
    /* 
    // Override to support rearranging the table view. 
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
    { 
    } 
    */ 
    
    /* 
    // Override to support conditional rearranging of the table view. 
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
    } 
    */ 
    
    #pragma mark - Table view delegate 
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
    } 
    
    @end 
    

回答

4

比方說你有一個名爲ARR的數組。

然後,您必須在調用numberOfRowsInSection時返回數組中的對象數。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [arr count]; 
} 

您的cellForRowAtIndexPath委託方法應該看起來像這樣打印出數組中的字符串。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Formal"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 

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

    return cell; 
} 

希望這有助於!

+0

這幫助我在路上了很多,還是花了一些時間來弄清楚這一切了,但現在我懂了。 :) – ingenspor

+0

@KristofferBilek考慮如果它幫助你,我的答案upvoting。謝謝! –

+0

對不起,我也是這個地方新來的。我試過了,但是我沒有足夠的聲望點去做。也許我應該將新的信息添加到您的答案中,而不是將其作爲新答案發布。 hmm – ingenspor

0

我的代碼現在看起來像這樣:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

omMeny = [[NSMutableArray alloc] init]; 

//Add items 
[omMeny addObject:@"Formålet med guiden"]; 
[omMeny addObject:@"Aromahjulet"]; 
[omMeny addObject:@"Fagterm"]; 


//Set the title 
self.navigationItem.title = @"Om Rødvin"; 
} 

//dealloc method declared in RootViewController.m 
- (void)dealloc { 

[omMeny release]; 
[super dealloc]; 
} 


// Uncomment the following line to preserve selection between presentations. 
// self.clearsSelectionOnViewWillAppear = NO; 

// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
// self.navigationItem.rightBarButtonItem = self.editButtonItem; 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#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 the number of rows in the section. 
// return 2; 
return [omMeny count]; 
} 

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

// Set up the cell... 
NSString *cellValue = [omMeny objectAtIndex:indexPath.row]; 
cell.textLabel.text = cellValue; 

// Configure the cell... 

return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Navigation logic may go here. Create and push another view controller. 
/* 
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
// ... 
// Pass the selected object to the new view controller. 
[self.navigationController pushViewController:detailViewController animated:YES]; 
*/ 
} 

@end 

這個在.H

@interface OmMasterViewController : UITableViewController { 

NSMutableArray *omMeny; 
} 
相關問題