TableView中添加一个新的标签自动改变 [英] TableView add a new Label that changes automatically

查看:339
本文介绍了TableView中添加一个新的标签自动改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我最近被达伦帮助解决我的tableview数据不显示的问题。我将离开这里一份文件,我将要到reffering

Ok so I was recently helped by Darren to solve a problem that my tableview data wasn't showing. I will leave a document here which I am going to be reffering to.

下载文件(3.12mb)

在故事板,在年底有3个对象,其中2会自动改变取决于依赖选择上的tableView什么项目。我想补充的另一个标签(在项目alread所做的那样)和SE这么一些信息会出现在这里取决于被选在表格视图什么项目。就像其他2

In the storyboard, at the end there are 3 object and 2 of them are automatically changing dependening on what item is chosen on the tableView. I would like to add another label (alread did in the project) and se it so some information will appear here depending on what item was chosen in the table view. Just like the other 2.

我怎样才能做到这一点?

How can I do this?

对于那些谁不信任下载文件时,我得到了一些code,可以帮助..我想在与标签#3标签上的myData2节目。我怎样才能做到这一点?

For those who don't trust the Download file, I got some code that might help.. I want to have the "myData2" show in a Label with Tag#3. How can I do this?

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


现在我得到一个断点:


Now I get a breakpoint:

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

The breakpoint is on the least line "[vc setSelectedItemInfo:[NSString stringWithFormat:@"%@", [myData2 objectAtIndex:selectedIndex]]];"

推荐答案

您需要创建Tab2_ItemViewController新的属性(而不是将selectedItem,使用说selectedItemInfo)。您还需要连接到要更新标签的新IBOutlet中。然后通过使用数据:

You need to create a new property in Tab2_ItemViewController (instead of selectedItem, use say selectedItemInfo). You will also need a new IBOutlet connected to the label you want to update. Then pass the data in using:

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

正是类似于您如何在数据传递的,你有其他的标签。

It is exactly analogous to how you are passing in data for the other label that you have.

我下载从你的链接code和它没有崩溃我。它被正确设定您所创建的新属性。该IBOutlet中的outputLabelInfo的UILabel没有挂接到故事板虽然。你需要把它挂起来。然后,你需要修正通过改变如何在此标签中的文本设置:

I downloaded the code from your link and it didn't crash for me. It was properly setting the new property that you created. The IBOutlet for the outputLabelInfo UILabel was not hooked up to the storyboard though. You need to hook it up. Then you need to fix how the text in this label is set by changing:

 [outputLabel setText:selectedItemInfo]; 

[outputLabelInfo setText:selectedItemInfo];

在Tab2_ItemViewController.m code。尝试这样做,清洗你的项目,然后再次运行。

in the Tab2_ItemViewController.m code. Try doing this, cleaning your project and running again.

这篇关于TableView中添加一个新的标签自动改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆