0

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!

4

1 回答 1

1

正如您所写,您想在选择单元格时设置标题,因此最好的方法是使用:

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

但是,您可以考虑将模型对象引入您的应用程序并将它们保存在某个数组中(objects在下面的示例中`。因此该方法如下所示:

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

}
于 2013-03-27T12:06:31.397 回答