TableView添加一个自动更改的新标签

好吧,最近我帮助Darren解决了我的tableview数据没有显示的问题。 我将在这里留下一份我将要讨论的文件。

下载文件(3.12mb)

在故事板中,最后有3个对象,其中2个自动更改,取决于tableView上select的项目。 我想添加另一个标签(在项目中已经做过),然后根据在表格视图中select的项目,在这里显示一些信息。 就像其他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 

现在我得到一个断点:

下载文件(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]]];”

您需要在Tab2_ItemViewController(而不是selectedItem,使用select selectedItemInfo)中创build一个新的属性。 您还需要连接到您要更新的标签的新IBOutlet。 然后使用传递的数据:

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

这与您传递其他标签的数据的方式完全相同。


我从你的链接下载了代码,并没有为我崩溃。 它正确地设置你创build的新的属性。 outputLabelInfo UILabel的IBOutlet并没有连接到故事板。 你需要把它挂起来。 然后,您需要修改如何通过更改设置此标签中的文本:

  [outputLabel setText:selectedItemInfo]; 

 [outputLabelInfo setText:selectedItemInfo]; 

在Tab2_ItemViewController.m代码中。 尝试这样做,清理你的项目,并再次运行。

顺便说一下,看看你的cellForRowAtIndexPath ,你在这里有一些好奇的构造。

如果您不使用单元格原型,但使用自己的控件,则必须在创build单元格时创build它们,例如:

 - (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; } 

我将留下具体的细节,如何以编程方式创build三个控件给你,但希望你明白了。 创build单元格时,您可以创build任何自定义控件(并设置它们各自的tag属性),并且如果您已成功取出单元格,则只需获取这些子视图的引用即可。

说实话,如果你只是处理图像,标题和副标题,我倾向于使用标准的UITableViewCell风格,并使用其textLabeldetailTextLabeldetailTextLabel属性,而不是做任何定制的单元格。 如果我执行自定义操作,则使用单元格原型和自定义子类,从而无需手动创build控件和/或使用viewWithTag手动检索对它们的viewWithTag