Search code examples
xcoderowtableviewcelltitle

How to change subview's title from a table selected row in Xcode?


Here is the problem: I have a table view, custom cells, and detail view. I have 10 rows in my table and when the user selects one of the rows, the detail view opens. How to set the detail view nav bar's title from the selected row of my table view? I have three classes - televisionList (my tableview), televisionCell (my cells in the table view) and televisionDetail (my detail view which opens when a row is selected). In my televisionCell I have declared the following label:

@property (weak, nonatomic) IBOutlet UILabel *tvLabel;

In my televisionList (the tableview) I have this method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    televisionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"televisionCell" owner:self options:nil];
        for (id currentObject in objects)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell = (televisionCell *)currentObject;
                break;
            }
        }

    }
switch (indexPath.row)
    {
        case 0:
        {
            cell.imageView.image = bnt;
            cell.tvLabel.text = @"БНТ 1";
            cell.backgroundView.image = [UIImage imageNamed:@"lightbackgr.png"];
            break;
        }

        case 1:
        {
            cell.imageView.image = btv;
            cell.tvLabel.text = @"bTV";
            cell.backgroundView.image = [UIImage imageNamed:@"background-cell.png"];
            break;
        }
        case 2:
        {
            cell.imageView.image = novatv;
            cell.tvLabel.text = @"Нова TV";
            cell.backgroundView.image = [UIImage imageNamed:@"lightbackgr.png"];
            break;
        }
}

Now, I want to change my detail view title to be equal to cell.tvLabel.text when a row is selected. Where and how to do that? Thanks in advance!


Solution

  • As you wrote, you want to set the title, when the cell is selected, so the best way will be to use:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        televisionCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        details.title=cell.tvLabel.text;    
    }
    

    However, you may consider introducing the model object to your app and keep them in some array(objects in the example below`. So the method would look like:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       YourObject* obj=[self.objects objectAtIndex:indexPath.row];
        details.title=obj.stationName;
    
    }