2015-11-23 40 views
0

我有一個帶有UITableViewDelegate的UITableViewUITableViewCell中的UIPickerView不加載數據

其中一種CellType由標籤和UITextField組成。

我已將UITextFieldinputView設置爲UIPickerView

的UIPickerView及其委託中的UITableViewDelegate

cellForRowAtIndexPath功能構造當我顯示我的表,創建tablerows,當我點擊的UITextField,一個選擇器彈出然而拾取器非沒有顯示填充的和我放入代表的日誌。

到目前爲止,我已經嘗試了一些東西,比如添加額外的刷新,但目前爲止沒有結果。

由於日誌沒有顯示我懷疑有委託和數據源的引用有問題嗎? PickerView的實例是否被銷燬?也許我忘了實現我沒有看到的方法?

任何幫助理解/修復這個讚賞!

截圖

screenshot

在我的代碼,這看起來是這樣的:

TableViewDelegate.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 
    cell = [self createListViewCell:indexPath tableView:tableView]; 
    return cell; 
} 

- (UITableViewCell *) createListViewCell:(NSIndexPath *)indexPath tableView:(UITableView *)tableView { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LIST_CELL_ID]; 
    ListTableCell *listTableCell = (ListTableCell *) cell; 
    List *list = [listsData objectAtIndex:indexPath.row]; 

    //Set List Label 
    listTableCell.listLabel.text = list.name; 

    //Create Value picker 
    UIPickerView *picker = [[UIPickerView alloc]init]; 
    //Create Delegate for picker 
    ListPickerViewDelegate *pickerDelegate = [[ListPickerViewDelegate alloc]init]; 

    pickerDelegate.data=list.itemData; 

    picker.dataSource = pickerDelegate; 
    picker.delegate = pickerDelegate; 

    listTableCell.listTextValue.inputView = picker; 
    [picker reloadAllComponents]; 

    return cell; 
} 

而且我ListPickerViewDelegate是這樣的:

ListPickerViewDelegate.h

#import <UIKit/UIKit.h> 

@interface ListPickerViewDelegate:NSObject <UIPickerViewDelegate,UIPickerViewDataSource> 

@property (nonatomic) NSArray *data; 
@property (nonatomic) UITextField *textField; 

@end 

ListPickerViewDelegate.m

#import "ListPickerViewDelegate.h" 
#import "DataItem.h" 

@implementation TagListPickerViewDelegate 
@synthesize data; 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 
    DataItem *thisDataItem = [data objectAtIndex:row]; 
    NSLog(@"Value for row is %@", thisDataItem.name); //Log not showing 

    return DataItem.name; 
} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    NSLog(@"Count for data is %li", (unsigned long)[data count]); //Log not showing 
    return [data count]; 
} 
@end 
+0

你的選擇器的y座標和框架在哪裏? –

+0

Picker是顯示在屏幕截圖屏幕底部的人。我是否需要爲此提供額外的參考? – RWIL

+0

然後在哪裏完成?我試圖達到什麼解釋在這裏:http://stackoverflow.com/a/20438343/3708094 – RWIL

回答

1

UIPickerViewdelegatedatasource性質weak定義。您的ListPickerViewDelegate對象在createListViewCell:tableView:方法的範圍內定義,因此它永遠不會被保留。

嘗試在TableViewDelegate內爲ListPickerViewDelegate創建一個強屬性,並將其初始化到其他位置(可能是在超載的init方法中)。然後只需將UIPickerViewdelegatedatasource分配給此屬性。

+0

感謝這個清晰的解釋,我會盡量保持我的TableViewDelegate PickerViewDelegates List並引用這些。如果它有效,我會將此答案標記爲已接受:) – RWIL