私はiOSアプリに少し手を加えてきましたが、それに投資する時間はあまりありません。これを機能させる方法や正しく構成されていないものがわからないため、壁に頭をぶつけています...NewsTableViewControllerで参照されている本文をNewsViewのtextViewにロードしようとしていますコントローラ。現在、タイトルのみが更新され、textviewにはlorumipsumテキストのみが表示されます。
私の理解では、NSLogで情報を見ることができ、プッシュされたビューのタイトルにニュース本文テキストを入れようとすると、タイトルに表示されます-ビューを定義できなかったと思いますが、私が言うように見えない!これがとにかく私が持っているものです...
これは、xmlファイルから最初のビューにデータをロードするテーブルです。
//
// NewsTableViewController.h
//
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
@interface NewsTableViewController : UITableViewController
{
IBOutlet UITableView *newsTable;
CGSize cellSize;
NSXMLParser *rssParser;
NSMutableArray *stories;
NSMutableDictionary *item;
NSString *currentElement;
NSMutableString *currentName, *currentTitle, *currentDated, *currentBodyText;
}
- (UITableViewCell *) getCellContentView:(NSString *)MyIdentifier;
@end
コードの実装
//
// NewsTableViewController.m
//
#import "NewsTableViewController.h"
#import "NewsViewController.h"
@interface NewsTableViewController ()
@end
@implementation NewsTableViewController
dispatch_queue_t myQueue;
-(void) showHUD{
MBProgressHUD *HUD;
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
//HUD.delegate = self;
HUD.labelText = @"News Loading";
HUD.detailsLabelText = @"please wait...";
HUD.square = YES;
HUD.dimBackground = YES;
[HUD showWhileExecuting:@selector(parserStart) onTarget:self withObject:nil animated:YES];
//dispatch_async(dispatch_get_main_queue(), ^ {[self parserStart]; });
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//NSLog(@"View Did Appear");
myQueue = dispatch_queue_create("com.xxxxxxxxxxx.xxxxxxxxxx",NULL);
dispatch_async(dispatch_get_main_queue(), ^ {[self showHUD]; });
}
- (void) parserStart {
//Insert a small delay for testing purposes
//[NSThread sleepForTimeInterval:2];
if ([stories count] == 0) {
NSString *path = @"http://xxx.xxxxxxxxxxx.xxx/xxxxxx/xxxxxxx.xml";
[self parseXMLFileAtURL:path];
//[path release];
}
cellSize = CGSizeMake([newsTable bounds].size.width, 60);
[self.tableView reloadData];
}
- (void)parseXMLFileAtURL:(NSString *)URL {
if (stories) {
//[stories release];
stories = nil;
}
stories = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
//NSLog(@"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download the news feed from web site (Error code %i )", [parseError code]];
//NSLog(@"error parsing XML: %@", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
//if (currentElement) {
//[currentElement release];
//currentElement = nil;
//}
currentElement = [elementName copy];
if ([elementName isEqualToString:@"article"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentName = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDated = [[NSMutableString alloc] init];
currentBodyText = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"article"]) {
[currentName appendString:string];
} else if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"dated"]) {
[currentDated appendString:string];
} else if ([currentElement isEqualToString:@"bodytext"]) {
[currentBodyText appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"article"]) {
// save values to an item, then store that item into the array...
[item setObject:currentName forKey:@"article"];
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentDated forKey:@"dated"];
[item setObject:currentBodyText forKey:@"bodytext"];
[stories addObject:[item copy]];
//NSLog(@"adding story: %@", currentName);
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [stories count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
cell = [self getCellContentView:MyIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *lblTitle = (UILabel *)[cell viewWithTag:101];
UILabel *lblDate = (UILabel *)[cell viewWithTag:102];
UILabel *lblBodyText = (UILabel *)[cell viewWithTag:103];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
//NSString *articleValue = [[stories objectAtIndex: storyIndex] objectForKey: @"article"];
NSString *titleValue = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSString *datedValue = [[stories objectAtIndex: storyIndex] objectForKey: @"dated"];
NSString *bodytextValue = [[stories objectAtIndex: storyIndex] objectForKey: @"bodytext"];
lblTitle.text = titleValue;
lblDate.text = datedValue;
lblBodyText.text = bodytextValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"NewsSegue"]) {
// note that "sender" will be the tableView cell that was selected
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NewsViewController *nvc = [segue destinationViewController];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
nvc.title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
nvc.textView.text = [[stories objectAtIndex: storyIndex] objectForKey: @"bodytext"];
//nvc.textView.text = [self getDataToPass:storyIndex.row];
// hide the tabBar Controller
nvc.hidesBottomBarWhenPushed = YES;
//NSLog(@"Article : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"article"]);
NSLog(@"Title : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"title"]);
NSLog(@"Dated : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"dated"]);
NSLog(@"BodyText : %@", [[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"]); }
}
- (void)dealloc {
}
@end
そして今、私が推し進めている見方...
//
// NewsViewController.h
//
#import <UIKit/UIKit.h>
@class NewsViewController;
@interface NewsViewController : UIViewController {
IBOutlet UITextView* textView;
}
@property (nonatomic, retain) IBOutlet UITextView* textView;
@end
そして、このビューの実装ファイル。
//
// NewsViewController.m
//
#import "NewsViewController.h"
#import "NewsTableViewController.h"
@interface NewsViewController ()
@end
@implementation NewsViewController
@synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textView = self.title;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
編集:私が理解していることから、セグエのこの部分は、パーサーに添付されたコードの送信部分内から情報を送信しているところです:
nvc.title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
nvc.textView.text = [[stories objectAtIndex:storyIndex] objectForKey: @"bodytext"];
情報が表示するタイトルとして本文を設定した場合、テキストビューに正しくないことがあると思うのはなぜですか?この点で行き詰まりますか?
何が悪いのかわからないので、助けていただければ幸いです!!! 私は実際に私が見逃したものが明白であることを望んでいます!見てくれてありがとう。