开发者

Returned web service data not populating tableview

开发者 https://www.devze.com 2023-02-07 15:29 出处:网络
Hey,I cant seem why this code is not working? I am trying to add my returned data from a web service into the uitableview, however failing miserably. The table shows up blank everytime. It seems it do

Hey, I cant seem why this code is not working? I am trying to add my returned data from a web service into the uitableview, however failing miserably. The table shows up blank everytime. It seems it doesnt like the cellForRowAtIndexPath method. But honestly I am not sure. I cant spot it for nothing. Please help. Thanks!

#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 appendDat开发者_StackOverflow中文版a: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


It appears you're ignoring warning messages, which is a no-no in Objective-C. The following code can't possibly work:

[newSongs addObject:[songs:i]]

What you probably meant to write was something like this:

[newSongs addObject:[songs objectAtIndex:i]]

But instead of doing all this:

newSongs = [[NSMutableArray alloc] init];

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

why not just do this?

newSongs = [songs mutableCopy];
0

精彩评论

暂无评论...
验证码 换一张
取 消