【问题标题】:Setting the title of a dynamic view from a table view从表格视图设置动态视图的标题
【发布时间】:2015-07-03 04:09:32
【问题描述】:

我正在使用 SWRevealViewController、A ContentViewController 和 NavigationViewController(导航)的组合。

故事板如下:

在 TableViewCell 上单击我可以获得导航文本。我想要做的就是将这些信息传递回自身。将 ContentViewController 的标题设置为选定的单元格并加载其他所需的数据。

我怎样才能实现这个功能?

这是我在 NavigationViewController.m 文件中的最新代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];


    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (isChild) {
        NSString *cellText = selectedCell.detailTextLabel.text;
        NSLog(@"%@ tapped",cellText);
        return;
    }
    else{
        NSString *cellText = selectedCell.textLabel.text;
        NSLog(@"%@ tapped",cellText);

        if([cellText isEqualToString:@"Sign Out"]){
            [self performSegueWithIdentifier:@"goToHome" sender:self];
        }
        else if ([cellText isEqualToString:@"Sign In"]){
            [self performSegueWithIdentifier:@"goToHome" sender:self];
        }
        else if ([cellText isEqualToString:@"Festival Map"]){
            [self performSegueWithIdentifier:@"festivalMap" sender:self];
        }
    }

    [self.tableView beginUpdates];

    if (currentExpandedIndex == indexPath.row) {
        [self collapseSubItemsAtIndex:currentExpandedIndex];
        currentExpandedIndex = -1;
    }
    else {

        BOOL shouldCollapse = currentExpandedIndex > -1;

        if (shouldCollapse) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
        }

        currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

        [self expandItemAtIndex:currentExpandedIndex];
    }

    [self.tableView endUpdates];

}



- (void)expandItemAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    NSArray *currentSubItems = [subItems objectAtIndex:index];
    int insertPos = index + 1;
    for (int i = 0; i < [currentSubItems count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
    }
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

- (void)collapseSubItemsAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"festivalMap"]){
        NSLog(@"TEST");

        NSString * title = @"CHANGE";

        ViewController * view = [segue destinationViewController];
        view.navigationItem.title = title;
    }
}

这是我的 ContentViewController.m 文件的代码:

#import "ContentViewController.h"
#import "SWRevealViewController.h"
#import "Function.h"

@interface ContentViewController ()

@end

@implementation ContentViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _barButton.target = self.revealViewController;
    _barButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

@end

谢谢

【问题讨论】:

  • 你要传递的数据是什么
  • 只是我从didSelectRowAtIndexPath 中得到的selectedCell.textLabel.text 并将其传递给ContentViewController
  • 您可以使用[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];获取您选择的单元格
  • 我已经这样做了......它正在传递有问题的数据并将其加载到 ContentViewController 中
  • prepareForSegue:sender: 中设置segue.destinationViewController.title 对您不起作用?

标签: ios xcode uiviewcontroller uinavigationcontroller segue


【解决方案1】:

你可以试试这个:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if([segue.identifier isEqualToString:@"yoursegue"]){  
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
    ContentViewController*contentView = (ContentViewController*)segue.destinationViewController;
    contentView.cellText = selectedCell.textLabel.text; 
    NSLog(@"cellText  = %@",selectedCell.textLabel.text;);
  }
}

但是,从用户界面访问值或围绕它构建逻辑并不是好的做法,而是应该使用数据源。这违反了 MVC 原则。

【讨论】:

  • 您能详细说明违反 MVC 原则的情况吗?我可以做些什么来遵循这些指南?
  • 加上这个解决方案不起作用!我在 ContentViewController.h 文件中添加了 :@property (weak,nonatomic) NSString * viewTitle; 并在 ContentViewController.m 文件中添加了 _viewTitle = self.title 对吗?
  • @Chris 作为替代方案,您也可以像属性一样在类级别声明cellText,然后访问segue中的值。你在日志中得到了什么
  • no.. 应用程序运行。但是导航栏标题的Title没有变化
  • @Chris 在类级别声明NSString *cellText,然后在两个地方使用NSLog 来查看值
猜你喜欢
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多