【问题标题】:How to use a segue when prototype cells are not working当原型单元不工作时如何使用segue
【发布时间】:2015-01-10 01:59:20
【问题描述】:

我有一个显示提要的 RSS 阅读器,然后当单击自定义单元格时,您应该会看到文章。一切正常,但后来我对其进行了更改,以便它可以与自定义单元格一起使用。为了做到这一点,我不得不从情节提要中删除所有原型单元格,随后删除了单元格和细节视图之间的segue。当我在应用程序中添加原型单元格时,会崩溃而没有任何错误。我很迷茫。如何在不使用原型单元或 segue 的情况下将其带到详细视图?我试过 didSelectRowAtIndexPath 但这是 UIViewController 不是 UITableViewController。我的表格视图代码如下,如果您有任何想法,请告诉我..

#import "APPMasterViewController.h"

    #import "APPDetailViewController.h"
    #import "IITableViewCell.h"

    @interface APPMasterViewController () {
        NSXMLParser *parser;
        NSMutableArray *feeds;
        NSMutableDictionary *item;
        NSMutableString *title;
        NSMutableString *link;
        NSString *element;
    }
    @end

    @implementation APPMasterViewController

    - (void)awakeFromNib
    {
        [super awakeFromNib];
    }

    - (void)viewDidLoad {
        [super viewDidLoad];
        [[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];

        feeds = [[NSMutableArray alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://example.com/?feed=rss"];
        parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        [parser setDelegate:self];
        [parser setShouldResolveExternalEntities:NO];
        [parser parse];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    #pragma mark - Table View

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return feeds.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *simpleTableIdentifier = @"IITableViewCell";

        IITableViewCell *cell = (IITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IITableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        cell.mainTextLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];

        return cell;
    }

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

        element = elementName;

        if ([element isEqualToString:@"item"]) {

            item    = [[NSMutableDictionary alloc] init];
            title   = [[NSMutableString alloc] init];
            link    = [[NSMutableString alloc] init];

        }

    }


    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

        if ([elementName isEqualToString:@"item"]) {

            [item setObject:title forKey:@"title"];
            [item setObject:link forKey:@"link"];

            [feeds addObject:[item copy]];

        }

    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

        if ([element isEqualToString:@"title"]) {
            [title appendString:string];
        } else if ([element isEqualToString:@"link"]) {
            [link appendString:string];
        }

    }

    - (void)parserDidEndDocument:(NSXMLParser *)parser {

        [self.tableView reloadData];

    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        return 78;
    }


    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"showDetail"]) {

            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
            [[segue destinationViewController] setUrl:string];

        }
    }

    @end

【问题讨论】:

  • 会有崩溃信息-尝试设置异常断点
  • 您是否重新制作了自定义单元原型的转场? segue 使用自定义单元格与标准 UITableViewCell 的工作方式没有区别。

标签: ios objective-c uitableview


【解决方案1】:

使用 UIViewController 没关系,只需要实现 UITableViewDelegate 协议即可:

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

您可以在 UIViewController 的 viewDidLoad 方法中设置委托

- (void)viewDidLoad {

  self.tableView.delegate = self;
  // UITableViewDataSource is another protocol you should implement
  self.tableView.dataSource = self;

}

您可以通过 CTRL 拖动到下一个视图控制器来设置 segue。确保您设置了 segue 的标识符,在本例中为“MySegue”

接下来实现 tableView:didSelectRowAtIndexPath:

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

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"MySegue" sender:cell];
}

在这个调用 segue 的方法中,你可以发送任何 id 作为发送者,在这种特殊情况下,我们发送的是单元格

之后,如果您想为 segue 做准备,请在任何 UIViewController 上实现以下可用方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"MySegue"]) {
        // prepare for segue here
    }
}

【讨论】:

    【解决方案2】:

    我不确定您的问题到底是什么,但作为一般规则,您可以通过在标识符输入中指定单元格的名称来添加自定义单元格,以便您可以在代码中引用它。

    然后,您可以 ctrl+单击到您的单元格,然后拖动到情节提要中您选择的详细视图控制器,选择所需的转场类型,然后单击转场并在属性中设置标识符控制板。你在代码中引用了这个segue,在方法@​​987654321@或performSegueWithIdentifier中。这能回答你的问题吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2013-02-10
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多