さて、私は最近、テーブルビュー データが表示されない問題を解決するために Darren に助けられました。参照する文書をここに残しておきます。
ストーリーボードでは、最後に 3 つのオブジェクトがあり、そのうちの 2 つは、tableView で選択されたアイテムに応じて自動的に変化します。私は別のラベルを追加したいと思います (プロジェクトで既に行われています) およびそれを使用して、テーブル ビューで選択されたアイテムに応じていくつかの情報がここに表示されるようにします。他の2と同じように。
これどうやってするの?
ダウンロード ファイルを信頼していない人のために、役立つコードをいくつか用意しました。Tag#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]]];"