gpt4 book ai didi

ios - 当原型(prototype)单元不工作时如何使用 segue

转载 作者:行者123 更新时间:2023-11-29 02:23:12 26 4
gpt4 key购买 nike

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

#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

最佳答案

如果你使用的是 UIViewController 并不重要,你只需要实现 UITableViewDelegate 协议(protocol)即可启用:

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

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

- (void)viewDidLoad {

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

}

您可以通过按住 CTRL 拖动到下一个 View Controller 来设置 Segue。确保您设置了 segue 的标识符,在本例中为“MySegue” Setting up segue

接下来实现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
}
}

关于ios - 当原型(prototype)单元不工作时如何使用 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com