如何将数组添加到TableViewController?

时间:2012-11-11 12:27:07

标签: ios cocoa-touch nsarray tableview

当我在互联网上冲浪时,我发现了很多如何在TableViewController中加载数组的例子,但是没有一个帮助过我!

你能不能帮我找到我的代码中的错误?

提前谢谢!!!

#import "Images.h"
#import "AFNetworking.h"
#import "HTMLparser.h"

@interface Images ()

@end

@implementation Images

@synthesize data;

@synthesize textFromVC1;
@synthesize tableView;

@synthesize so2;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.data = [[NSMutableArray alloc]init];

    NSError *error = nil;

    NSString *so = [@"http://" stringByAppendingString: self.textFromVC1];

    NSURL *url = [[NSURL alloc]initWithString:so];

    NSString *str=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    HTMLParser *parser = [[HTMLParser alloc] initWithString:str error:&error];

    if (error) {
        NSLog(@"Error: %@", error);
        [parser release];
        parser = nil;
        return;
    }

    HTMLNode * body = [parser body];

    NSArray *inputs = [body findChildTags:@"img"];

    for (HTMLNode *input in inputs) {

        NSString *links = [input getAttributeNamed:@"src"];

        NSString *so1 = [so stringByAppendingString: @"/"];

        so2 = [so1 stringByAppendingString: links];


        [self.data addObject:so2];



            NSLog(@"%@", data);



        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
            {



       } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)

      {

          }];

        [operation start];

    }

   [self.tableView reloadData];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.data.count;
}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }




 cell.textLabel.text = [self.data objectAtIndex:indexPath.row];

     [self.tableView reloadData];

    return cell;

}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

- (void)dealloc {
    [tableView release];
    [super dealloc];
}
@end

2 个答案:

答案 0 :(得分:0)

好1)numberOfSections是0.看到它应该是一个IIRC 认为这样,但没有仔细检查

答案 1 :(得分:0)

假设您的data数组已正确构建,那么您的错误就是numberOfSections方法。目前,它返回0,告诉UITableView当前没有要显示的部分......永远。

你可以在那里return 1;或完全删除整个方法。您从UITableViewController继承的默认实现默认情况下也会返回1

我的建议是删除该方法,因为这可以使您自己的代码更清晰。您可以随时在实际需要时实施它。