-2

好的,最近 Darren 帮助我解决了我的 tableview 数据没有显示的问题。我将在此处留下我将要参考的文件。

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

现在我得到一个断点:

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

4

2 回答 2

0

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

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

这与您为您拥有的其他标签传递数据的方式完全相似。


我从您的链接下载了代码,但对我来说并没有崩溃。它正确设置了您创建的新属性。outputLabelInfo UILabel 的 IBOutlet 并没有连接到情节提要。你需要把它挂起来。然后,您需要通过更改来修复此标签中文本的设置方式:

 [outputLabel setText:selectedItemInfo]; 

[outputLabelInfo setText:selectedItemInfo];

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

于 2013-02-20T05:32:56.867 回答
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,detailTextLabelimageView属性,而不是对单元格进行任何自定义。如果我确实执行了自定义,我会使用单元原型和自定义子类,这样就无需手动创建控件和/或使用viewWithTag.

于 2013-02-22T06:10:29.767 回答