gpt4 book ai didi

objective-c - NSURLConnection 成功后重新加载 TableView

转载 作者:行者123 更新时间:2023-11-29 05:00:01 25 4
gpt4 key购买 nike

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

我有一个 TableView ,一旦 NSURLConnection 成功,我将尝试重新加载该 View 。我有许多消息可以帮助我指导我......但是当我在 TableView 上调用重新加载时,表不会重新填充。

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

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

最佳答案

在connectionDidFinishLoading中更改如下:

theTweets = allTweets;

对此:

self.theTweets = allTweets;

如果您愿意,也可以这样:

[self setTheTweets:allTweets];

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

关于objective-c - NSURLConnection 成功后重新加载 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com