简体   繁体   English

表单元进入视图控制器

[英]Table cell into view controller

I have a tableview that displays a newsfeed with parser rss. 我有一个表视图,显示带有解析器rss的新闻源。 I want the top cell to be displayed in another view controller. 我希望顶部的单元格显示在另一个视图控制器中。 Imagine it as if you have a newsfeed and you want to post the latest news on the homepage. 想象一下,好像您有新闻源,并且想要在首页上发布最新新闻。 How do i solve this?! 我该如何解决? I have not found anything alike on the web and therefor asking this question. 我在网上找不到任何类似的东西,因此提出了这个问题。

Note that this code is working fine for gathering the rss feed. 请注意,此代码可以很好地收集rss feed。 I want to ''copy'' the first cell and display it on the homepage. 我想“复制”第一个单元格并将其显示在主页上。

@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

Don't try to copy a cell, at least a cell instance. 不要尝试复制一个单元格,至少要复制一个单元格实例。 The cell instances are managed by the table view. 单元实例由表视图管理。 Moreover, there is nothing to copy, probably. 而且,可能没有要复制的内容。 You just want to display the same data twice or at 2 different places. 您只想将相同的数据显示两次或在2个不同的位置显示。

You should decouple your understanding of a view with that of a model. 您应该将对视图的理解与模型的理解脱钩。 If you want to use a "cell" inside another view controller, tell that other (table)viewcontroller to use the same kind of cell, and once it requests it (after a reloadData for instance), fill that cell with the same model data. 如果要在另一个视图控制器中使用“单元格”,请告诉其他(表)视图控制器使用相同类型的单元格,并在请求它(例如在reloadData之后)后,用相同的模型数据填充该单元格。

Don't try to copy the cell,instead try to get the data of that cell and make a custom view like a table cell and paste the data in that custom view . 不要尝试复制单元格,而要尝试获取该单元格的数据并像表单元格一样创建自定义视图,然后将数据粘贴到该custom view If I were you,I will make a custom view that looks like a table cell and will parse the data to that view. 如果您是我,我将创建一个看起来像表格单元格的custom view ,并将数据解析到该视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM