データ@"0"、@ "1"、@ "2"、@ "3" ...を使用してUIPickerViewを行に表示しようとしています。これは、2列のpickerViewになります。
これが私のコードです:
これは.hファイルです
#import <UIKit/UIKit.h>
@interface CustomNumberViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *numberPicker;
@property(strong, nonatomic)NSArray *listOfNumbers;
@property (strong,nonatomic)NSString *numberOfFirstColumn;
@property(strong,nonatomic)NSString *numberOfSecondColumn;
@end
これは.mファイルです
#import "CustomNumberViewController.h"
@interface CustomNumberViewController ()
@end
@implementation CustomNumberViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
//UIView background
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"pickerbackground.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
//init picker
self.numberPicker.showsSelectionIndicator = TRUE;
self.listOfNumbers = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Number of rows to display in each component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [self.listOfNumbers count];
}
return [self.listOfNumbers count];
}
//Number of columns to display
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//define what to display in each rows and columns
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0) {
return [self.listOfNumbers objectAtIndex:row];
}
return [self.listOfNumbers objectAtIndex:row];
}
//selected number to be stored in nsstring
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component==0) {
self.numberOfFirstColumn = [self.listOfNumbers objectAtIndex:row];
}
self.numberOfSecondColumn = [self.listOfNumbers objectAtIndex:row];
}
@end
私の問題は、アプリを実行しようとすると、UIPickerViewにデータが入力されないため、完全に空になります。「選択したインジケーター」をスカルしようとすると、アプリがすぐにクラッシュします...これはエラーメッセージです-[UITableViewRowDatarectForRow:inSection:]、/ SourceCache / UIKit_Sim / UIKit-2380.17 / UITableViewRowData.m:1630のアサーションの失敗
キャッチされなかった例外'NSInternalInconsistencyException'が原因でアプリを終了しています、理由:'無効なインデックスパス(2インデックス[0、0])でのrectのリクエスト'
助言がありますか ?ありがとう