1
我想添加一個今天的擴展名,它顯示了一個來自RSS源的列表,比如我的應用的部分功能。我的問題是它沒有顯示數據。今天的小部件擴展沒有顯示數據
.H
#import <UIKit/UIKit.h>
@class RSSChannel;
@interface TodayViewController : UIViewController <NSXMLParserDelegate, UITableViewDataSource, UITableViewDelegate> {
NSURLConnection *connection;
NSMutableData *xmlData;
RSSChannel *channel;
}
@property (weak, nonatomic) IBOutlet UITableView *widgetTableView;
- (void)fetchEntries;
@end
.M
#import "TodayViewController.h"
#import "RSSChannel.h"
#import "RSSItem.h"
#import <NotificationCenter/NotificationCenter.h>
@interface TodayViewController() <NCWidgetProviding>
@end
@implementation TodayViewController
@synthesize widgetTableView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self fetchEntries];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
completionHandler(NCUpdateResultNewData);
}
- (void)fetchEntries
{
xmlData = [[NSMutableData alloc]init];
NSURL *url = [NSURL URLWithString:@"http://kyfbnewsroom.com/category/ag-news/feed"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
connection = [[NSURLConnection alloc]initWithRequest:req delegate:self startImmediately:YES];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:@"channel"])
{
channel = [[RSSChannel alloc]init];
[channel setParentParserDelegate:self];
[parser setDelegate:channel];
}
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
[xmlData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
xmlData = nil;
NSMutableArray *actionAlerts = [NSMutableArray array];
for (RSSItem *object in channel.items)
{
if (object.isActionAlert)
{
[actionAlerts addObject:object];
}
}
for (RSSItem *object in actionAlerts)
{
[channel.items removeObject:object];
}
// Reload the table
[[self widgetTableView]reloadData];
}
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
{
connection = nil;
xmlData = nil;
}
# pragma mark - Table View Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[channel items]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RSSItem *item = [[channel items]objectAtIndex:[indexPath row]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
cell.textLabel.font = [UIFont systemFontOfSize:16.0];
}
if (cell) {
cell.textLabel.text = [item title];
cell.textLabel.textColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
@end