我使用GMNString + Html和RMXElement以及RSSItem和RSSLoader文件创建了一个iOS RSS阅读器应用程序。 现在我在我的表中有帖子标题和说明。我想知道如何在我的描述下添加帖子发布日期和时间。请具体,因为我是一个Begginer:D 更新:这是我的文件.RSSItem.m:
#import "RSSItem.h"
#import "GTMNSString+HTML.h"
@implementation RSSItem
-(NSAttributedString*)cellMessage
{
if (_cellMessage!=nil) return _cellMessage;
NSDictionary* boldStyle = @{NSFontAttributeName: [UIFont fontWithName:@"Futura-CondensedMedium" size:16.0]};
NSDictionary* normalStyle = @{NSFontAttributeName: [UIFont fontWithName:@"GillSans-LightItalic" size:16.0]};
NSMutableAttributedString* articleAbstract = [[NSMutableAttributedString alloc] initWithString:self.title];
[articleAbstract setAttributes:boldStyle
range:NSMakeRange(0, self.title.length)];
[articleAbstract appendAttributedString:
[[NSAttributedString alloc] initWithString:@"\n\n"]
];
int startIndex = [articleAbstract length];
NSString* description = [NSString stringWithFormat:@"%@...", [self.description substringToIndex:100]];
description = [description gtm_stringByUnescapingFromHTML];
[articleAbstract appendAttributedString:
[[NSAttributedString alloc] initWithString: description]
];
[articleAbstract setAttributes:normalStyle
range:NSMakeRange(startIndex, articleAbstract.length - startIndex)];
_cellMessage = articleAbstract;
return _cellMessage;
}
@end
RSSItem.h:
#import <Foundation/Foundation.h>
@interface RSSItem : NSObject
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* description;
@property (strong, nonatomic) NSURL* link;
@property (strong, nonatomic) NSAttributedString* cellMessage;
@end
RSSLoader.h:
#import <Foundation/Foundation.h>
typedef void (^RSSLoaderCompleteBlock)(NSString* title, NSArray* results);
@interface RSSLoader : NSObject
-(void)fetchRssWithURL:(NSURL*)url complete:(RSSLoaderCompleteBlock)c;
@end
RSSLoader.m:
#import "RSSLoader.h"
#import "RXMLElement.h"
#import "RSSItem.h"
@implementation RSSLoader
-(void)fetchRssWithURL:(NSURL*)url complete:(RSSLoaderCompleteBlock)callbackFunction
{
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
(void)[conn initWithRequest:request delegate:self];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([data length] == 0 && error == nil) {
// handle empty response
} else if (error != nil) {
// handle error
NSLog(@"Error %@", [error localizedDescription]);
} else if ([httpResponse statusCode] == 200) {
RXMLElement *rss = [RXMLElement elementFromXMLData:data];
RXMLElement *rssChild = [rss child:@"channel"];
RXMLElement* title = [rssChild child:@"title"];
NSArray* items = [[rss child:@"channel"] children:@"item"];
NSMutableArray* result = [NSMutableArray arrayWithCapacity:items.count];
//more code
for (RXMLElement *e in items) {
//iterate over the articles
RSSItem* item = [[RSSItem alloc] init];
item.title = [[e child:@"title"] text];
item.description = [[e child:@"description"] text];
item.link = [NSURL URLWithString: [[e child:@"link"] text]];
if (item.link != NULL) {
[result addObject: item];
}
}
callbackFunction([title text], result);
}
}];
}
@end
MasterViewController.m:
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "TableHeaderView.h"
#import "RSSLoader.h"
#import "RSSItem.h"
@interface MasterViewController ()
{
NSArray *_objects;
NSURL* feedURL;
UIRefreshControl* refreshControl;
}
@end
@implementation MasterViewController
{
NSMutableArray *tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//configuration
self.title = @"آخرین اخبار";
feedURL = [NSURL URLWithString:@"http://appleapps.ir/feed"];
//add refresh control to the table view
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self
action:@selector(refreshInvoked:forState:)
forControlEvents:UIControlEventValueChanged];
NSString* fetchMessage = [NSString stringWithFormat:@"در حال بروزرسانی %@",feedURL];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:fetchMessage
attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:11.0]}];
[self.tableView addSubview: refreshControl];
//add the header
self.tableView.tableHeaderView = [[TableHeaderView alloc] initWithText:@"در حال بروزرسانی..."];
[self refreshFeed];
}
-(void) refreshInvoked:(id)sender forState:(UIControlState)state
{
[self refreshFeed];
}
-(void)refreshFeed
{
RSSLoader* rss = [[RSSLoader alloc] init];
[rss fetchRssWithURL:feedURL
complete:^(NSString *title, NSArray *results) {
//completed fetching the RSS
dispatch_async(dispatch_get_main_queue(), ^{
//UI code on the main queue
[(TableHeaderView*)self.tableView.tableHeaderView setText:title];
_objects = results;
[self.tableView reloadData];
// Stop refresh control
[refreshControl endRefreshing];
});
}];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
RSSItem *object = _objects[indexPath.row];
cell.textLabel.attributedText = object.cellMessage;
cell.textLabel.numberOfLines = 0;
[cell.textLabel setTextAlignment:UITextAlignmentRight];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
RSSItem *item = [_objects objectAtIndex:indexPath.row];
CGRect cellMessageRect = [item.cellMessage boundingRectWithSize:CGSizeMake(200,200000)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return cellMessageRect.size.height;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RSSItem *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
@end