0

当我在网上冲浪时,我发现了很多如何在 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
4

2 回答 2

0

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

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

我的建议是删除该方法,因为这样可以使您自己的代码更干净。您总是可以在稍后阶段实现它,当您可能真正需要它时。

于 2012-11-11T12:52:26.233 回答
0

好吧 1) numberOfSections 是 0。看到的东西应该是一个 IIRC 认为就是这样,但没有仔细检查

于 2012-11-11T12:46:18.937 回答