使用xcode 6.1编译时未调用cellForRowAtIndexPath

时间:2014-11-19 16:35:48

标签: ios uitableview xcode6

在我的应用程序中,我有一个带有[table view] [1]的弹出窗口。

使用Xcode 5.1编译时,一切正常,但使用Xcode 6.1编译的相同代码无法调用[cellForRowAtIndexPath] [3] [delegate] [4]方法。

调用其他代表方法。

一个重点是self.tableView.rowHeight;返回-1

我已尝试将委托和数据源明确设置为self,但这没有区别

该类由以下代码调用;

`-(IBAction)selectLanguage:(id)sender
{
    ATLMLanguagePopoverTableViewController *pvc = [[ATLMLanguagePopoverTableViewController alloc] initWithNibName:nil bundle:nil];
    pvc.target = self;
    pvc.action = @selector(popoverDidSelectItem:);
    pvc.items = [[[ATLMLibraryManager getManager]libraryDefaults]getAvailableLanguageNames];
    _myPopoverController.contentViewController = pvc;
    [_myPopoverController setPopoverContentSize:[pvc popoverContentSize]];
    [_myPopoverController presentPopoverFromBarButtonItem:(UIBarButtonItem *)sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
}
` 

听到是班级的定义


/

/ LanguagePopoverTableViewController.m // SalesAid // // Created by phuang on 1/16/13. // Copyright (c) 2013 Align Technology. All rights reserved. // #import "ATLMLanguagePopoverTableViewController.h" #import "ATLMLocalizationManager.h" #import "ATLMUtils.h" @interface ATLMLanguagePopoverTableViewController () @end @implementation ATLMLanguagePopoverTableViewController @synthesize items, selectedItem, target, action; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { selectedItem = -1; target = nil; action = NULL; } return self; } -(void) resetLocalization { [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)setItems:(NSArray *)newItems { items = [newItems copy]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidUnload { [super viewDidUnload]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; selectedItem=0; NSString *curLang = (NSString *) [[ATLMLocalizationManager getManager]getCurrentLanguage] ; for(int i = 0; i < items.count ; i++ ){ if([curLang isEqualToString:(NSString *)[items objectAtIndex:i]]){ selectedItem = i; break; } } NSIndexPath *i = [NSIndexPath indexPathForRow:selectedItem inSection:0]; [self.tableView selectRowAtIndexPath:i animated:NO scrollPosition:UITableViewScrollPositionNone]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } - (CGSize)popoverContentSize { NSInteger rowHeight = self.tableView.rowHeight; UITableView *tv = self.tableView; rowHeight = 50; return CGSizeMake(100, [items count] * rowHeight); } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [items count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.font = [UIFont systemFontOfSize:14]; UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; myBackView.backgroundColor = [ATLMUtils getAlignBlue]; cell.selectedBackgroundView = myBackView; [cell setSelectedBackgroundView: myBackView ]; NSString *textLabelKey = [items objectAtIndex:[indexPath indexAtPosition:1]]; cell.textLabel.text = ATLMLocalizedString(textLabelKey, nil); cell.textLabel.textAlignment = NSTextAlignmentCenter; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectedItem = indexPath.row; if (target != nil && action != NULL) { [target performSelector:action withObject:self]; } } @end

`

1 个答案:

答案 0 :(得分:0)

好的回答我自己的问题;

基本上,代码在调用init方法后将模型添加到视图控制器。但是看起来线程模型已经改变了一点,并且在添加模型之前创建了视图,因此模型中的行数为零

解决方案是将模型作为init方法的一部分传递。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil items:(NSArray *)itemArray
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        items = [itemArray copy];
        selectedItem = -1;
        target = nil;
        action = nil;
    }
    return self;
}