我有一个RSS阅读器,它显示提要,然后单击自定义单元格时,将带您到本文。一切正常,但随后我对其进行了更改,以便可以与自定义单元一起使用。为此,我必须从情节提要中删除所有原型单元,然后再删除该单元和详细视图之间的序列。当我在应用程序中添加原型单元时,崩溃时没有任何错误。我很困扰。如何不使用原型单元或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
最佳答案
使用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
}
}
关于ios - 原型(prototype)单元不工作时如何使用segue,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27872092/