2012-07-19 36 views
0

試圖讓我的RSS源應用程序具有標籤內的摘要,我已經解析了描述(摘要),但我不明白如何將它添加到UITableViewCell中。我目前使用的是UITableViewCellStyleSubtitle,所以我可以顯示每篇文章的標題和下面的日期。 如何在其下面添加一個以顯示每篇文章的摘要?如何讓我的UITableViewCell不僅有標題和副標題,還有標題,副標題(日期)和另一個標籤(摘要)?

MasterViewController.h(TableView中頭)

#import <UIKit/UIKit.h> 

@class RSSChannel; 
@class WebViewController; 

@interface MasterViewController : UITableViewController 
<NSXMLParserDelegate> 
{ 
    NSURLConnection *connection; 
    NSMutableData *xmlData; 

    RSSChannel *channel; 
} 
- (void)fetchEntries; 
@property (nonatomic, strong) WebViewController *webViewController; 

@end 

MasterViewController.m(TableView中試行)

#import "MasterViewController.h" 
#import "RSSChannel.h" 
#import "RSSItem.h" 
#import "WebViewController.h" 


@interface MasterViewController() { 
    NSMutableArray *_objects; 
} 
@end 

@implementation MasterViewController 
@synthesize webViewController; 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [self fetchEntries]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 


- (void)insertNewObject:(id)sender 
{ 
    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

#pragma mark - Table View 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"The amount of items in the table: %u", [[channel items] count]); 
    return [[channel items] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:@"UITableViewCell"]; 

    } 
    RSSItem *item = [[channel items] objectAtIndex:[indexPath row]]; 

    [[cell textLabel] setText:[item title]]; 
    [[cell detailTextLabel] setText:[item date]]; 
    return cell; 
} 


- (void)fetchEntries 
{ 
    // Create a new data container for the stuff that comes back from the service 
    xmlData = [[NSMutableData alloc] init]; 

    // Construct a URL that will ask the service for what you want - 
    // Note we can concatenate literal strings together on multiple lines in this way it 
    // results in a single NSString instance 
    NSURL *url = [NSURL URLWithString: 
        @"http://sephardijews.com/feed/"]; 

    // Putting the URL we made into an NSURLRequest, so we can connect to the url data that we specifed 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Creating a connecting that will exchange this request for the data from the URL we specifed 
    connection = [[NSURLConnection alloc] initWithRequest:req 
               delegate:self 
             startImmediately:YES]; 
} 

- (void)parser:(NSXMLParser *)parser 
    didStartElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI 
    qualifiedName:(NSString *)qualifedName 
    attributes:(NSDictionary *)attributeDict 
{ 
    NSLog(@"%@ found a %@ element", self, elementName); 
    if ([elementName isEqual:@"channel"]) { 

     // If the parser saw a channel, create new instance, store in our ivar 
     channel = [[RSSChannel alloc] init]; 

     // Give the channel object a pointer back to ourselves for later 
     [channel setParentParserDelegate:self]; 

     // Set the parser's delegate to the channel object 
     [parser setDelegate:channel]; 
    } 
} 

// Method used to start at the beginning of the instance of the tableview, it will intialize the connection method to retrieve the posts from the URL 

/* - (id) initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 

    if (self) { 
     [self fetchEntries]; 
    } 

    return self; 
} 
*/ 

// This method will be called several times as the data arrives 
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data 
{ 
    // Add the incoming chunk of data to the container we are keeping 
    // The data always comes in the correct order 
    [xmlData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)conn 
{ 
    // Create the parser object with the data received from the web service 
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData]; 

    // Give it a delegate - don't worry about the warning 
    [parser setDelegate:self]; 

    // Tell it to start parsing - the documet will be parsed and the delegate of NSXMLParser will get all of its delegate messages sent to it before this line finishes execution - it is blocking 
    [parser parse]; 

    // Get rid of the XML data as we no longer need it 
    xmlData = nil; 

    // Get rid of the connection, no longer need it 
    connection = nil; 

    // Reload the table 
    [[self tableView] reloadData]; 
    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]); 

} 

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error 
{ 
    // Release the connection object, we are done with it cause' there is no connection 
    // Setting the connection to nil will stop the connection because it is nothing/0 
    connection = nil; 

    // Release the xmlData object. We stopped to connection to put the data in the xmlData object, so we set it to nil also 
    xmlData = nil; 

    // Grab the description of the error object passed to us, so we can tell the user 
    NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]]; 

    // Create and show an alert view to the user with the error string to tell them the error in the process of the connection 
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" 
               message:errorString 
               delegate:nil 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 

    [av show]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Push the web view controller onto the navigation stack - this implicitly 
    // creates the web view controller's view the first time through 
    [[self navigationController] pushViewController:webViewController animated:YES]; 

    //Grab the selected item 
    RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]]; 

    //Constructs a URL with the link string of the item 
    NSURL *url = [NSURL URLWithString:[entry link]]; 

    // Construct a request object with that URL 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Load the request into the web view 
    [[webViewController webView] loadRequest:req]; 

    // Set the title of the web view controller's navifation item 
    [[webViewController navigationItem] setTitle:[entry title]]; 

} 



/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

@end 

回答

0

嘗試改變UITableViewCellStyle定製,並在自己的UITextLabel拖動對象到原型單元。讓@property S代表每個在你TableViewController數據項...

@interface MasterViewController() 

    NSMutableArray *_objects; 
    @property (nonatomic, strong) IBOutlet UILabel *title; 
    @property (nonatomic, strong) IBOutlet UILabel *date; 
    @property (nonatomic, strong) IBOutlet UILabel *summary; 

@end 

然後UITextLabels連接到各自的數據。

相關問題