【问题标题】:TableView add a new Label that changes automaticallyTableView 添加一个自动更改的新标签
【发布时间】:2013-02-04 17:36:27
【问题描述】:

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

Download File (3.12mb)

在故事板中,最后有 3 个对象,其中 2 个根据在 tableView 上选择的项目自动更改。我想添加另一个标签(已经在项目中)并设置它,以便根据在表格视图中选择的项目,此处会显示一些信息。就像其他 2 一样。

我该怎么做?


对于那些不信任下载文件的人,我得到了一些可能会有所帮助的代码。我想让“myData2”显示在带有标签#3 的标签中。我该怎么做?

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

现在我得到一个断点:

Download File (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]]];"

【问题讨论】:

    标签: ios automation label tableview


    【解决方案1】:

    顺便说一下,看看你的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 样式并使用它的textLabeldetailTextLabelimageView 属性,并且不对单元格进行任何自定义。如果我执行自定义,我会使用单元原型和自定义子类,这样就无需手动创建控件和/或使用viewWithTag 手动检索对它们的引用。

    【讨论】:

      【解决方案2】:

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

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

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


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

       [outputLabel setText:selectedItemInfo]; 
      

      [outputLabelInfo setText:selectedItemInfo];
      

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

      【讨论】:

      • 我是否还需要添加一个“@property (nonatomic, retain) NSString *selectedItemInfo;”在底部?当我添加 oyu 给我的代码时,我得到一个“使用未声明的标识符 'vc'”
      • 好的,现在我在你给我的代码处得到了一个断点。这是文件的链接。你能给它最后一次检查吗?我会非常感谢达伦。 mediafire.com/?415fwprd58pwf70
      • 对不起,darren,但我如何将 IBOutlet 连接到 outputlabelinfo?这是你在编辑中描述的吗?
      • 好的,所以我把它连接起来,按照你说的做了。但是仍然在我之前告诉你的代码行处得到那个断点:S
      • 恭喜,很高兴你成功了!关于电子邮件,但我现在无法获得私人帮助,但正如我在另一个问题中所写的那样,您可以继续在此处发帖,也许我会看到问题或其他人可以帮助您。祝你好运!
      猜你喜欢
      • 2021-10-30
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2014-04-14
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多