2013-05-31 99 views
-1

我通過WCF調用獲取了一些ShoppingLists的列表。UITableView單元格未被填充

我的代碼:當我運行在Xcode應用

#import "ListTableViewController.h" 
#import "ListServiceSvc.h" 

@interface ListTableViewController(){ 
    // NSMutableArray *shoppingLists; 
} 
@property (nonatomic, retain) NSMutableArray *shoppingList; 

@end 

@implementation ListTableViewController 

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

- (void)viewDidLoad 
{ 
    _shoppingList = [NSMutableArray array]; 
    [self loadListsFromRemoteServer]; 
    [super viewDidLoad]; 
} 

-(void) loadListsFromRemoteServer 
{ 
    NSString *uname = @"Test4"; 
    NemListBinding *binding = [[ListServiceSvc NemListBinding]initWithAddress:@"http://balder.ucn.dk/dm76_gr5/WCF.ListService.svc/custom?singleWsdl"]; 
    binding.logXMLInOut=YES; 
    ListServiceSvc_GetShoppingListsWithUname *parms = [[ListServiceSvc_GetShoppingListsWithUname alloc]init]; 
    parms.uname = uname; 
    [binding GetShoppingListsWithUnameAsyncUsingParameters:parms delegate:self]; 
} 

- (void) operation:(NemListBindingOperation *)operation completedWithResponse:(NemListBindingResponse *)response 
{ 
    NSArray *responseHeaders = response.headers; 
    NSArray *responseBodyParts = response.bodyParts; 
    //[NSThread sleepForTimeInterval:5.0]; 
    NSMutableArray *shoppingListFromWebserver = [[NSMutableArray alloc] init]; 
    // step 1 fill in the blanks. 
    for(id header in responseHeaders) { 
     // here do what you want with the headers, if there's anything of value in them 
    } 
    for (id mine in responseBodyParts) 
    { 
     if ([mine isKindOfClass:[ListServiceSvc_GetShoppingListsWithUnameResponse class]]) 
     { 
      for (id slist in [[mine GetShoppingListsWithUnameResult] ShoppingList]) 
      { 
       [shoppingListFromWebserver addObject:slist]; 
       NSLog(@"new list :: RESPONSE FROM SERVER :: nList %@", [slist ShoppingListName]); 
       //self.result.text = [self.result.text stringByAppendingString:[slist shoppingListName]]; 
      } 
     } 
    } 
    [self performSelectorOnMainThread:@selector(updateNewView:) withObject:shoppingListFromWebserver waitUntilDone:NO]; 
} 

-(void) updateNewView:(NSMutableArray*) result 
{ 
    _shoppingList = result; 
    NSLog(@"new list - number of news :: %u", [_shoppingList count]); 
    [self.tableView reloadData]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
//{ 
// return _shoppingList.count; 
//} 

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
//{ 
// // Return the number of rows in the section. 
// return 10; 
//} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"cellforrow"); 
    static NSString *CellIdentifier = @"ListCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    // Configure the cell... 

    id shopList = [_shoppingList objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [shopList ShoppingListName]; 
    return cell; 

} 

什麼也沒有發生。我可以在日誌窗口中的日誌

NSLog(@"new list - number of news :: %u", [_shoppingList count]); 

確實返回一個值(4),但NSLog我已經在CellForRowAtIndexPath方法制成的不打印它看到的。我也有一個斷點,沒有達到。爲什麼它沒有得到該方法?

回答

4

這些方法,numberOfSectionsInTableViewnumberOfRowsInSection ..它應該是這個樣子是很重要的,確定的TableView應該表現出什麼樣的數據的數量numberOfSectionsInTableView和numberOfRowsInSection,你tableView將是0節和0行,所以方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 

沒有被訪問。

+0

非常感謝你 - 不知道我錯過了什麼 – catu

0

你不應該註釋掉這些方法:numberOfSectionsInTableViewnumberOfRowsInSection

至少需要numberOfRowsInSection,並且必須返回您想要/需要的行數。 就我所知,這應該是_shoppingList.count你的情況。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; // the number of different sections in your table view 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [_shoppingList count]; // the number of data in your shopping list 
}