未填充UITableView单元格

时间:2013-05-31 07:11:37

标签: ios uitableview

我通过ShoppingLists电话获取了一些WCF的列表。

我的代码:

#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;

}

在Xcode中运行应用程序时没有任何反应。我可以在日志窗口中看到日志

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

会返回值(4),但我在NSLog方法中创建的CellForRowAtIndexPath不会被打印出来。我也有一个断点,但没有到达。为什么不采用这种方法?

3 个答案:

答案 0 :(得分:4)

这些方法numberOfSectionsInTableViewnumberOfRowsInSection对于确定TableView应显示的数据量非常重要..它应该如下所示:

- (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
}

答案 1 :(得分:0)

您不应该注释掉这些方法:numberOfSectionsInTableViewnumberOfRowsInSection

至少需要numberOfRowsInSection,并且必须返回您想要/需要的行数。 就我而言,在你的情况下,这应该是_shoppingList.count

答案 2 :(得分:0)

因为你注释掉了这些方法:numberOfSectionsInTableView和numberOfRowsInSection,你的tableView将是0部分和0行,所以方法

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

未被访问。