2013-02-19 43 views
-2

好的,我最近幫助Darren解決了我的tableview數據沒有顯示的問題。我將在這裏留下一份我將要討論的文件。TableView添加一個自動更改的新標籤

Download File (3.12mb)

在情節串連圖板,在端部有3個對象以及它們的2自動地改變取決於依賴選擇上的tableView什麼項目。我想添加另一個標籤(在項目中做過讀取),然後選擇它,以便在此處顯示一些信息,具體取決於表格視圖中選擇的項目。就像其他2.

我該怎麼做?


對於那些誰不信任下載文件,我得到了一些代碼,可以幫助..我想在與標籤#3標籤上的「myData2」節目。我怎樣才能做到這一點?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Define our test data 
    myData = [NSMutableArray arrayWithObjects: 
       @"Chasing Amy", 
       @"Mallrats", 
       @"Dogma", 
       @"Clerks", 
       @"Jay & Silent Bob Strike Back", 
       @"Red State", 
       @"Cop Out", 
       @"Jersey Girl", 
       nil]; 

    //test for 2nd data 
    myData2 = [NSMutableArray arrayWithObjects: 
       @"Info for Chasing Amy item", 
       @"Info for Mallrats", 
       @"Info for Dogma", 
       @"Info for Clerks", 
       @"Info for Jay & Silent Bob Strike Back", 
       @"Info for Red State", 
       @"Info for Cop Out", 
       @"Info for Jersey Girl", 
       nil]; 
} 

// Return number of sections in table (always 1 for this demo!) 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

// Return the amount of items in our table (the total items in our array above) 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [myData count]; 
    return [myData2 count]; 
} 

// Return a cell for the table 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // A cell identifier which matches our identifier in IB 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Create or reuse a cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Get the cell label using it's tag and set it 
    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; 
    [cellLabel setText:[myData objectAtIndex:indexPath.row]]; 

    // get the cell imageview using it's tag and set it 
    UIImageView *cellImage = (UIImageView *)[cell viewWithTag:2]; 
    [cellImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.row]]]; 

    return cell; 
} 

// Do some customisation of our new view when a table item has been selected 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure we're referring to the correct segue 
    if ([[segue identifier] isEqualToString:@"ShowSelectedMovie"]) { 

     // Get reference to the destination view controller 
     Tab2_ItemViewController *vc = [segue destinationViewController]; 

     // get the selected index 
     NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; 

     // Pass the name and index of our film 
     [vc setSelectedItem:[NSString stringWithFormat:@"%@", [myData objectAtIndex:selectedIndex]]]; 
     [vc setSelectedIndex:selectedIndex]; 

     // 
     [vc setSelectedItem:[NSString stringWithFormat:@"%@", [myData2 objectAtIndex:selectedIndex]]]; 
     [vc setSelectedIndex:selectedIndex]; 
    } 
} 

@end 

現在,我得到一個斷點:

Download File (3.12mb)

#import "Tab2_TableViewController.h" 
#import "Tab2_ItemViewController.h" 

@implementation Tab2_TableViewController 

// When the view loads, define our data 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Define our test data 
    myData = [NSMutableArray arrayWithObjects: 
       @"Chasing Amy", 
       @"Mallrats", 
       @"Dogma", 
       @"Clerks", 
       @"Jay & Silent Bob Strike Back", 
       @"Red State", 
       @"Cop Out", 
       @"Jersey Girl", 
       nil]; 

    // Define our test data2 
    myData2 = [NSMutableArray arrayWithObjects: 
       @"info Chasing Amy", 
       @"info Mallrats", 
       @"info Dogma", 
       @"info Clerks", 
       @"info Jay & Silent Bob Strike Back", 
       @"info Red State", 
       @"info Cop Out", 
       @"info Jersey Girl", 
       nil]; 
} 



// Return the amount of items in our table (the total items in our array above) 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [myData count]; 
    return [myData2 count]; 
} 

// Return number of sections in table (always 1 for this demo!) 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

// Return a cell for the table 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // A cell identifier which matches our identifier in IB 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Create or reuse a cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Get the cell label using its tag and set it 
    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; 
    [cellLabel setText:[myData objectAtIndex:indexPath.row]]; 

    // Get the cell label2 using its tag and set it 
    UILabel *cellLabelInfo = (UILabel *)[cell viewWithTag:3]; 
    [cellLabelInfo setText:[myData2 objectAtIndex:indexPath.row]]; 

    // get the cell imageview using its tag and set it 
    UIImageView *cellImage = (UIImageView *)[cell viewWithTag:2]; 
    [cellImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.row]]]; 

    return cell; 
} 

// Do some customisation of our new view when a table item has been selected 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure we're referring to the correct segue 
    if ([[segue identifier] isEqualToString:@"ShowSelectedMovie"]) { 

     // Get reference to the destination view controller 
     Tab2_ItemViewController *vc = [segue destinationViewController]; 

     // get the selected index 
     NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; 

     // Pass the name and index of our film 
     [vc setSelectedItem:[NSString stringWithFormat:@"%@", [myData objectAtIndex:selectedIndex]]]; 
     [vc setSelectedIndex:selectedIndex]; 
     [vc setSelectedItemInfo:[NSString stringWithFormat:@"%@", [myData2 objectAtIndex:selectedIndex]]]; 
    } 
} 

@end 

斷點是最少行 「[VC setSelectedItemInfo:[的NSString stringWithFormat:@」 %@」, [myData2 objectAtIndex:selectedIndex]]];「

回答

0

您需要在Tab2_ItemViewController(而不是selectedItem,使用select selectedItemInfo)中創建一個新屬性。您還需要連接到您要更新的標籤的新IBOutlet。然後傳遞使用中的數據:

[vc setSelectedItemInfo:[NSString stringWithFormat:@"%@", [myData2 objectAtIndex:selectedIndex]]]; 

這與您爲其他標籤傳遞數據的方式完全相同。


我從鏈接下載了代碼,它並沒有讓我崩潰。它正確地設置了您創建的新屬性。雖然輸出標籤信息UILabel的IBOutlet沒有連接到故事板。你需要把它連接起來。然後,你需要修正通過更改在此標籤的文本設置:

[outputLabel setText:selectedItemInfo]; 

[outputLabelInfo setText:selectedItemInfo]; 

在Tab2_ItemViewController.m代碼。嘗試這樣做,清理您的項目並再次運行。

+0

我是否還需要添加一個「@property(nonatomic,retain)NSString * selectedItemInfo;」在底部?當我添加一段代碼oyu給了我,我得到一個「使用未聲明的標識符'vc'」 – AlexPjz 2013-02-20 22:33:10

+0

沒想到第二個問題,我想出了。 – AlexPjz 2013-02-20 22:42:37

+0

好的,現在我在你給我的代碼中得到一個斷點。這是一個指向文件的鏈接。你可以給它最後一張支票嗎?我會很感激達倫。 http://www.mediafire.com/?415fwprd58pwf70 – AlexPjz 2013-02-20 22:55:54

0

順便說一句,看看你的cellForRowAtIndexPath,你在這裏有一些好奇的構造。

如果你不使用電池的原型,但你使用自己的控件,那麼你必須在創建細胞,例如創建它們:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // A cell identifier which matches our identifier in IB 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Create or reuse a cell 
    UILabel *cellLabel; 
    UILabel *cellLabelInfo; 
    UIImageView *cellImage; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cellLabel = [[UILabel alloc] init...]; 
     cellLabel.tag = 1; 
     cellLabelInfo = [[UILabel alloc] init...]; 
     cellLabelInfo.tag = 3; 
     cellImage = [[UIImageView alloc] init...]; 
     cellImage.tag = 2; 
    } 
    else 
    { 
     cellLabel = (UILabel *)[cell viewWithTag:1]; 
     cellLabelInfo = (UILabel *)[cell viewWithTag:3]; 
     cellImage = (UIImageView *)[cell viewWithTag:2]; 
    } 

    // Get the cell label using its tag and set it 
    [cellLabel setText:[myData objectAtIndex:indexPath.row]]; 

    // Get the cell label2 using its tag and set it 
    [cellLabelInfo setText:[myData2 objectAtIndex:indexPath.row]]; 

    // get the cell imageview using its tag and set it 
    [cellImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.row]]]; 

    return cell; 
} 

當我離開的細節你如何以編程方式創建三個控件,但希望你明白。在創建單元格時,您可以創建任何自定義控件(並分別設置它們的tag屬性),並且如果您已成功取出單元格,則只需引用這些子視圖即可。

說實話,如果你只是處理圖片,標題和副標題,我會傾向於使用標準UITableViewCell風格,並使用其textLabeldetailTextLabelimageView性質,而不是做的任何定製細胞。如果我執行定製,我使用單元格原型和自定義子類,這樣就不需要手動創建控件和/或使用viewWithTag手動檢索對它們的引用。