表格单元进入视图控制器

时间:2015-04-19 10:38:05

标签: ios objective-c xcode uitableview

我有一个tableview,显示带有解析器rss的新闻源。我希望顶部单元格显示在另一个视图控制器中。想象一下,好像你有一个新闻源,你想在主页上发布最新消息。我该如何解决这个问题?! 我没有在网上找到任何相似内容,因此提出这个问题。

请注意,此代码适用于收集RSS Feed。我想复制'''''第一个单元格并将其显示在主页上。

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

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"https://feedity.com/dinskolmat-    se/VFJSUFJa.rss"];
                            //http://clownfish.skolpytt.appspot.com/ostra-real/rss
                            //https://feedity.com/dinskolmat-se/VFJSUFJa.rss"];

parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[self.view     addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

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

#pragma mark - Table view data source
-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader";
UITableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (headerView == nil){
    [NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"];
}
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 90.0f;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return feeds.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.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];
}

-(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];
}
}
- (IBAction)homebutton:(id)sender {

}
@end

2 个答案:

答案 0 :(得分:0)

不要尝试复制一个单元格,至少是一个单元格实例。单元实例由表视图管理。而且,可能没有什么可复制的。您只想两次或在两个不同的地方显示相同的数据。

您应该将对视图的理解与模型的理解分开。如果你想在另一个视图控制器中使用“单元格”,告诉其他(表)视图控制器使用相同的种类的单元格,并且一旦它请求它(例如在reloadData之后),填充具有相同模型数据的单元格。

答案 1 :(得分:0)

不要尝试复制单元格,而是尝试获取该单元格的数据并创建自定义视图(如表格单元格)并将数据粘贴到custom view中。如果我是你,我会创建一个看起来像表格单元格的custom view,并将数据解析到该视图。

相关问题