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

.NET Framework IT屋
问 题

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.

Download File (3.12mb)

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?


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:

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

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

解决方案

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.


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]; 

to

[outputLabelInfo setText:selectedItemInfo];

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

本文地址:IT屋 » TableView add a new Label that changes automatically

问 题

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

下载文件(3.12mb)

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

我怎样才能做到这一点?


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

   - (无效)viewDidLoad中
{
    [超级viewDidLoad中];    //定义我们的测试数据
    MYDATA的= [NSMutableArray里arrayWithObjects:
              @“猜情寻”
              @“Mallrats”
              @“教条”,
              @“科员”,
              @“杰伊&安培;鲍伯的回击”,
              @“红州”,
              @“警察去”,
              @“泽西女​​孩”,
              零];    //测试数据第2
    myData2 = [NSMutableArray里arrayWithObjects:
              @“信息为追逐艾米项目”
              @“信息为Mallrats”
              @“信息的教条”
              @“信息为科员”,
              @“信息周杰伦&放大器;鲍伯的回击”,
              @“信息为红州”,
              @“信息为警察去”,
              @“信息为泽西女孩”,
              零];
}//返回段的数量表(始终为1这个演示!)
- (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView
{
    返回1;
}//返回我们的表项目金额(数组中的总项以上)
- (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分
{
    返回[myData的数];
    返回[myData2计数]
}//返回一个单元格表
- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这我们在IB标识符匹配的小区标识符
    静态的NSString * CellIdentifier = @“CellIdentifier”    //创建或重用细胞
    * UITableViewCell的细胞= [的tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    如果(细胞==零){
        电池= [[UITableViewCell的页头] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }    //使用它的标签获取细胞的标签,并将其设置
    的UILabel * cellLabel =(的UILabel *)[细胞viewWithTag:1];
    [cellLabel的setText:[myData的objectAtIndex:indexPath.row]];    //使用它的标签获得细胞的ImageView,并设置
    *的UIImageView = cellImage(UIImageView的*)[细胞viewWithTag:2];
    [cellImage setImage:[UIImage的imageNamed:[的NSString stringWithFormat:@“%D.JPG”,indexPath.row]]];    返回细胞;
}//做我们的新观点的一些定制当一个表项已选定
- (无效)prepareForSegue:(UIStoryboardSegue *)赛格瑞发件人:(ID)发送者
{
    //确保我们指的是正确的SEGUE
    如果([SEGUE符] isEqualToString:@“ShowSelectedMovie”]){        //获取参考目标视图控制器
        Tab2_ItemViewController * VC = [赛格瑞destinationViewController]        //获取选定的索引
        NSInteger的的selectedIndex = [[self.tableView indexPathForSelectedRow]排]        //把电影的名字和索引
        [VC setSelectedItem:[的NSString stringWithFormat:@“%@”,[myData的objectAtIndex:的selectedIndex]]];
        [VC的setSelectedIndex:的selectedIndex]。        //
        [VC setSelectedItem:[的NSString stringWithFormat:@“%@”,[myData2 objectAtIndex:的selectedIndex]]];
        [VC的setSelectedIndex:的selectedIndex]。
    }
}@结束

现在我得到一个断点:

下载文件(3.12mb)

 #进口“Tab2_TableViewController.h”
#进口“Tab2_ItemViewController.h”@implementation Tab2_TableViewController//当视图加载,定义我们的数据
- (无效)viewDidLoad中
{
    [超级viewDidLoad中];    //定义我们的测试数据
    MYDATA的= [NSMutableArray里arrayWithObjects:
              @“猜情寻”
              @“Mallrats”
              @“教条”,
              @“科员”,
              @“杰伊&放大器;放大器;鲍伯的回击”,
              @“红州”,
              @“警察去”,
              @“泽西女​​孩”,
              零];    //定义我们的测试数据2
    myData2 = [NSMutableArray里arrayWithObjects:
              @“信息猜情寻”
              @“信息Mallrats”
              @“信息教条”
              @“信息科员”
              @“信息周杰伦&放大器;放大器;鲍伯的回击”,
              @“信息红河州”
              @“资讯警察去”,
              @“信息泽西女孩”,
              零];
}//返回我们的表项目金额(数组中的总项以上)
- (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分
{
    返回[myData的数];
    返回[myData2计数]
}//返回段的数量表(始终为1这个演示!)
- (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView
{
    返回1;
}//返回一个单元格表
- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这我们在IB标识符匹配的小区标识符
    静态的NSString * CellIdentifier = @“CellIdentifier”    //创建或重用细胞
    * UITableViewCell的细胞= [的tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    如果(细胞==零){
        电池= [[UITableViewCell的页头] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }    //使用其标签获取细胞的标签,并将其设置
    的UILabel * cellLabel =(的UILabel *)[细胞viewWithTag:1];
    [cellLabel的setText:[myData的objectAtIndex:indexPath.row]];    //使用其标签获取细胞LABEL2并设置它
    的UILabel * cellLabelInfo =(的UILabel *)[细胞viewWithTag:3];
    [cellLabelInfo的setText:[myData2 objectAtIndex:indexPath.row]];    //使用其标签中获取细胞的I​​mageView,并设置
    *的UIImageView = cellImage(UIImageView的*)[细胞viewWithTag:2];
    [cellImage setImage:[UIImage的imageNamed:[的NSString stringWithFormat:@“%D.JPG”,indexPath.row]]];    返回细胞;
}//做我们的新观点的一些定制当一个表项已选定
- (无效)prepareForSegue:(UIStoryboardSegue *)赛格瑞发件人:(ID)发送者
{
    //确保我们指的是正确的SEGUE
    如果([SEGUE符] isEqualToString:@“ShowSelectedMovie”]){        //获取参考目标视图控制器
        Tab2_ItemViewController * VC = [赛格瑞destinationViewController]        //获取选定的索引
        NSInteger的的selectedIndex = [[self.tableView indexPathForSelectedRow]排]        //把电影的名字和索引
        [VC setSelectedItem:[的NSString stringWithFormat:@“%@”,[myData的objectAtIndex:的selectedIndex]]];
        [VC的setSelectedIndex:的selectedIndex]。
        [VC setSelectedItemInfo:[的NSString stringWithFormat:@“%@”,[myData2 objectAtIndex:的selectedIndex]]];
    }
}@结束

断点是最不在线“[VC setSelectedItemInfo:[的NSString stringWithFormat:@”%@“,[myData2 objectAtIndex:的selectedIndex]]];”


解决方案

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

  [VC setSelectedItemInfo:[的NSString stringWithFormat:@“%@”,[myData2 objectAtIndex:的selectedIndex]]];

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


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

  [outputLabel的setText:selectedItemInfo];

  [outputLabelInfo的setText:selectedItemInfo];

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

本文地址:IT屋 » TableView中添加一个新的标签自动改变