2011-02-01 48 views
0

嘿,我看不出爲什麼這段代碼不工作?我試圖將我從web服務返回的數據添加到uitableview中,但是失敗了。該表每次顯示空白。它似乎不喜歡cellForRowAtIndexPath方法。但老實說,我不確定。我什麼也找不到。請幫忙。謝謝!返回的Web服務數據沒有填充tableview

#import "RSSTableViewController.h" 


@implementation RSSTableViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
if (self = [super initWithStyle:style]) { 
    songs = [[NSMutableArray alloc] init]; 
} 
return self; 
} 



- (void)loadSongs 
{ 

[songs removeAllObjects]; 
[[self tableView] reloadData]; 

// Construct the web service URL 
NSURL *url =[NSURL URLWithString:@"http://localhost/get_params"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url 
             cachePolicy:NSURLRequestReloadIgnoringCacheData 
            timeoutInterval:30]; 

if (connectionInProgress) { 
    [connectionInProgress cancel]; 
    [connectionInProgress release]; 
} 

[xmlData release]; 
xmlData = [[NSMutableData alloc] init]; 

connectionInProgress = [[NSURLConnection alloc] initWithRequest:request 
                 delegate:self]; 
} 
- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [self loadSongs]; 
} 

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
[connection release]; 

NSString *responseString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]; 

songs = [responseString componentsSeparatedByString:@","]; 

newSongs = [[NSMutableArray alloc] init]; 

for(int i=0; i < [songs count]; i++) { 
    [newSongs addObject:[songs:i]]); 
} 
    [songs autorelease]; 


[[self tableView] reloadData]; 
// 
} 

- (void)connection:(NSURLConnection *)connection 
    didFailWithError:(NSError *)error 
{ 
    [connectionInProgress release]; 
    connectionInProgress = nil; 

    [xmlData release]; 
    xmlData = nil; 

    NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", 
         [error localizedDescription]]; 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:errorString 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
               destructiveButtonTitle:nil 
                otherButtonTitles:nil]; 
    [actionSheet showInView:[[self view] window]]; 
    [actionSheet autorelease]; 

    [[self tableView] reloadData]; 
} 


- (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)viewDidUnload { 
    [super viewDidUnload]; 

} 


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

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [newSongs count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

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

    [[cell textLabel] setText:[newSongs objectAtIndex:[indexPath row]]]; 



    return cell; 
} 

@end 
+0

爲什麼不在Web服務之後放置一些日誌記錄,以便看到返回的內容?對於單個問題,這是太多的代碼 - 您需要縮小問題範圍。 – 2011-02-01 00:41:37

+0

我做到了。數據按預期填充數組。這發生在connectionDidFinishLoading方法中。之後,uitableview方法不希望將數據添加到tableview。 – toneb 2011-02-01 01:03:03

回答

2

看來你忽略了警告消息,這在Objective-C中是一個不允許的。下面的代碼不可能工作:

[newSongs addObject:[songs:i]] 

你大概意思寫的是這樣的事情是什麼:

[newSongs addObject:[songs objectAtIndex:i]] 

但是在做這一切:

newSongs = [[NSMutableArray alloc] init]; 

for(int i=0; i < [songs count]; i++) { 
    [newSongs addObject:[songs:i]]); 
} 

爲什麼不只是做這個?

newSongs = [songs mutableCopy];