今天Widget扩展不显示数据

时间:2014-10-30 14:20:45

标签: ios uitableview ios8-today-widget

我正在尝试添加今天的扩展程序,该扩展程序显示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

enter image description here

1 个答案:

答案 0 :(得分:0)

您不应该在tableView:cellForRowAtIndexPath委托方法中清除文本标签的文本颜色。 尝试分配例如白色:

cell.textLabel.textColor = [UIColor whiteColor];
相关问题