好的,最近 Darren 帮助我解决了我的 tableview 数据没有显示的问题。我将在此处留下我将要参考的文件。
在情节提要中,最后有 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
现在我得到一个断点:
#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]]];"