NSURLConnection成功后重新加载表视图

时间:2011-08-24 22:22:45

标签: objective-c ios xcode ios4

我是Xcode的新手,所以请耐心等待:

我有一个表视图,我试图在NSURLConnection成功后重新加载。我有很多消息可以帮助我一路引导我...但是当我在表格视图上调用重新加载时,表格不会重新填充。

JsonViewController.h:

#import <UIKit/UIKit.h>

@interface JsonViewController : UITableViewController {
    NSMutableArray *theTweets;
    IBOutlet UITableView *tview;
    NSMutableData *responseData;
}

@property (nonatomic, retain) NSMutableArray *theTweets;
@property (nonatomic, retain) UITableView *tview;

@end

JsonViewController.m:

#import "JsonViewController.h"
#import "SBJson.h"

@implementation JsonViewController
@synthesize theTweets;
@synthesize tview;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
     // Custom initialization
    }
    return self;
}

- (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) dealloc {
    [theTweets release];
    [super dealloc];

}

- (NSMutableArray*)theTweets {
    return [[theTweets retain] autorelease];
}

- (void) setTheTweets:(NSMutableArray *)newTweets {
    if (newTweets != theTweets) {
        [newTweets retain];
        [theTweets release];
        theTweets = newTweets;
        NSLog(@"Setting new tweets...");
        [tview reloadData]; 

    }
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    tview.delegate = self;

    responseData = [[NSMutableData data] retain];
    theTweets = [NSMutableArray array];
    NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"http://search.twitter.com/search.json?q=AriaPoker&result_type=recent"]];

    [[NSURLConnection alloc] initWithRequest: request delegate:self];
    NSLog(@"Trying to get feed upon initialization");
}

- (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

// methods that are not important

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"Number of the tweets count at this point: %d", [theTweets count]); 
    return [theTweets count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSLog(@"Number of the tweets count at this point: %d", [theTweets count]); 

    // Configure the cell...
    NSDictionary *aTweet = [theTweets objectAtIndex:[indexPath row]];
    //cell.textLabel.text = [aTweet objectForKey:@"text"];
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.numberOfLines = 4;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

    cell.textLabel.text = @"Test";
    cell.detailTextLabel.text = @"haha";

    //NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"profile_image_url"]];
    //NSData *data = [NSData dataWithContentsOfURL:url];
    //cell.imageView.image = [UIImage imageWithData:data];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;    

    return cell;
        NSLog(@"Loading cells in table");
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

}

#pragma mark NSURLConnection Delegate Methods
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //do nothing
    NSLog(@"A connection error has occurred!");
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *results = [[responseString JSONValue] retain];
    NSLog(@"Number of Rows: %d", [results count]);


    NSMutableArray *allTweets = [results objectForKey:@"results"];

    //[viewController setTweets:allTweets];

    theTweets = allTweets;

    NSLog(@"Number of misc2: %d", [theTweets count]); 
    [results release];
    [tview reloadData];

}

@end

我想知道我在这里做错了什么。

2 个答案:

答案 0 :(得分:1)

在connectionDidFinishLoading中改变:

theTweets = allTweets;

到此:

self.theTweets = allTweets;

或者这样,如果您愿意:

[self setTheTweets:allTweets];

您没有调用setter方法,因此没有保留。

答案 1 :(得分:0)

正如progrmr所建议的那样尝试调用setter方法,或者只是在你尝试设置属性时将 theTweets 属性的定义更改为@dynamic theTweets,自定义setter方法将是调用。