好的,所以我最近帮助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]]];”
答案 0 :(得分:0)
您需要在Tab2_ItemViewController中创建一个新属性(而不是selectedItem,请使用say selectedItemInfo)。您还需要一个新的IBOutlet连接到您要更新的标签。然后使用:
传递数据[vc setSelectedItemInfo:[NSString stringWithFormat:@"%@", [myData2 objectAtIndex:selectedIndex]]];
这与您传递其他标签的数据完全类似。
我从你的链接下载了代码,它并没有让我崩溃。它正确设置了您创建的新属性。尽管如此,outputLabelInfo UILabel的IBOutlet并没有连接到故事板。你需要把它搞定。然后,您需要通过更改:
来修复此标签中文本的设置方式 [outputLabel setText:selectedItemInfo];
到
[outputLabelInfo setText:selectedItemInfo];
在Tab2_ItemViewController.m代码中。尝试这样做,清理项目并再次运行。
答案 1 :(得分: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
样式并使用其textLabel
,detailTextLabel
和imageView
属性,而不是对单元格进行任何自定义。如果我执行自定义,我会使用单元格原型和自定义子类,这样就无需手动创建控件和/或使用viewWithTag
手动检索对它们的引用。