2012-11-11 78 views
0

當我在互聯網上衝浪時,我發現了很多示例如何在TableViewController中加載數組,但是沒有一個能夠幫助我!如何將數組添加到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 

回答

0

以及1)numberOfSections爲0。看到的東西應該是一個IIRC 認爲多數民衆贊成,但還沒有雙重檢查

+0

對不起,我沒有得到它 – Costa

+0

方法tableView:numberOfSections返回0 - >含義:表中什麼都沒有,返回1 :) - (在編譯指示警告「incomplete」之下); –

+1

這個答案是正確的。我自己的回答擴大了一點。 – SpacyRicochet

0

假設你data陣列正確建起來了,你的錯誤在你numberOfSections方法。目前,它返回0,它告訴UITableView目前沒有要顯示的部分......曾經。

您可以選擇return 1;那裏或完全刪除整個方法。從UITableViewController繼承的默認實現默認也返回1

我的建議是刪除該方法,因爲這樣可以保持自己的代碼更清潔。你可以隨時在後續階段實施,當你真的需要它時。

+0

我刪除了它,但沒有發生任何事情。我不知道!我已經過度搜索,我什至不能找到任何幫助 – Costa

+0

你確定數據數組是正確的嗎?請NSLog我們的陣列 –

+1

,並確保self.tableView不是零 –