2011-08-24 16 views
0

我是新來的Xcode後,如此忍受我:重裝表視圖NSURLConnection的成功

我有我想重裝一次的成功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 

我不知道我在做什麼錯在這裏。

+0

當你運行所有這些時會發生什麼?哪些NSLog消息丟失(我認爲是症狀)? – progrmr

+0

當我調用你建議的setter方法後,我沒有得到任何消息在' - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'method' - 我想到的方法會觸發'[tview reloadData];'? – javisrk

回答

1

,在由該connectionDidFinishLoading變化:

theTweets = allTweets; 

這樣:

self.theTweets = allTweets; 

還是這樣,如果你喜歡:

[self setTheTweets:allTweets]; 

你沒有調用setter方法,所以它沒有被保留。

+0

我已經調用了setter方法,但我仍然沒有得到任何東西。 – javisrk

+0

經過多一點研究後,我發現'[tview reloadData]'是不正確的 - 我沒有'tview'正確分配給表視圖。由於我沒有使用InterfaceBuilder來創建表格視圖,因此'[self.tableView]'做了竅門! – javisrk

0

至於建議的progrmr試圖調用setter方法,或者乾脆改變theTweets屬性@dynamic theTweets在這種情況下的定義,當您嘗試設置屬性,自定義setter方法將被調用。