TableView add a new Label that changes automatically

两盒软妹~` 提交于 2020-01-07 09:33:14

问题


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


回答1:


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.




回答2:


By the way, looking at your cellForRowAtIndexPath, you have some curious constructions here.

If you're not using cell prototypes, but you're using your own controls, then you have to create them when you create the cell, e.g.:

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

I'll leave the specifics of how you programmatically create the three controls up to you, but hopefully you get the idea. You create any of your custom controls (and set their respective tag properties) when you create the cell, and if you successfully dequeued the cell, you just need to get reference those subviews.

To be honest, if you're just dealing with image, title, and subtitle, I'd be inclined to use the standard UITableViewCell style and use its textLabel, detailTextLabel and imageView properties, and not do any customization of the cell. And if I do perform customization, I use cell prototypes and custom subclasses, which eliminates the need for manually creating controls and/or manually retrieving references to them with viewWithTag.



来源:https://stackoverflow.com/questions/14967643/tableview-add-a-new-label-that-changes-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!