sách gpt4 ai đã đi

ios - UIPickerView 依赖于 UITableView

In lại 作者:行者123 更新时间:2023-12-01 19:10:57 30 4
mua khóa gpt4 Nike

我在编程方面没有太多经验,所以这是我的问题:

我正在尝试编写一个转换器应用程序。最后,您可以输入一个数字。然后我有两个组件UIPickerView .使用第一个组件,您可以选择输入格式(例如 °Celcius)。使用第二个组件,您可以选择输出格式(例如°Fahrenheit),以便将°Celcius 转换为°Fahrenheit。

但我想添加更多单位(重量、能量等)。我希望它们显示在 UITableView 上.如果我在 UITableView 中选择某个单元格, UIPickerView必须更新其委托(delegate)。更准确地说,包含单位名称(例如 eV、Joule、cal、erg 等)的数组必须以某种方式在 detailViewController 中实现。 (我有我的 UIPickerView ).

Đây là mã của tôi:

View Controller .h

#nhập 

@interface ViewController : UIViewController


@property (strong,nonatomic) IBOutlet UIPickerView *EinheitenPicker;
@property (weak,nonatomic) IBOutlet UITableView *EinheitenTableView;

@kết thúc

View Controller .m
#import "ViewController.h"


@interface ViewController ()
{
NSArray *EinheitenNameArray;
}
@kết thúc

@implementation ViewController
@synthesize EinheitenPicker,EinheitenTableView;
- (void)viewDidLoad
{
[super viewDidLoad];

EinheitenNameArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AlleEinheiten" ofType:@"plist"]];

[self.EinheitenTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];

[self.EinheitenTableView.delegate tableView:self.EinheitenTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

#pragma mark UItableView Delegate
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return EinheitenNameArray.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"CellId";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text=[[EinheitenNameArray objectAtIndex:indexPath.row] objectForKey:@"Titel"];

return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//NSLog(@"Chosen:%@",self.EinheitenPicker);

[self.storyboard instantiateViewControllerWithIdentifier:@"TestId"];

[self.EinheitenPicker reloadAllComponents];
}

#pragma mark UIPickerView Methods

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

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSIndexPath *selectedIndexPath=[self.EinheitenTableView indexPathForSelectedRow];
NSArray *units=[[EinheitenNameArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Einheiten"];

if (component == 0) {
return units.count;
}

return units.count;
}

- (NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSIndexPath *selectedIndexPath=[self.EinheitenTableView indexPathForSelectedRow];
NSArray *units=[[EinheitenNameArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Einheiten"];
return [units objectAtIndex:row];
}

@kết thúc

1 Câu trả lời

如果我没记错的话,你正在寻找这样的东西。

nhập mô tả hình ảnh ở đây

要拥有它,您必须按如下方式实现 UIPickerView 方法

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

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.section] objectForKey:@"units"];
return units.count;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.section] objectForKey:@"units"];
return [units objectAtIndex:row];
}

hiện hữu UITableView 委托(delegate) 您可以按如下方式重新加载选择器 View 。
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.unitPicker reloadAllComponents];
}

创建项目具有组件和单元的表 ,您可以使用 .plist 文件如下

nhập mô tả hình ảnh ở đây

为了帮助您使用 UITableView 方法,我添加了所需的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
unitDataArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource: ofType:@"plist"]];
[self.unitsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.unitsTableView.delegate tableView:self.unitsTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

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

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *unitData=[unitDataArray objectAtIndex:section];
return [[unitData objectForKey:@"units"] count];
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"CellId";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[[[unitDataArray objectAtIndex:indexPath.section] objectForKey:@"units"] objectAtIndex:indexPath.row];

return cell;
}

-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSDictionary *unitData=[unitDataArray objectAtIndex:section];
return [unitData objectForKey:@"title"];
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.unitPicker reloadAllComponents];
}

更改
如果您希望它如下所示,则需要对我的代码进行细微调整。

nhập mô tả hình ảnh ở đây
@implementation ViewController
@synthesize unitPicker,unitsTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
unitDataArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AllUnits" ofType:@"plist"]];
[self.unitsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.unitsTableView.delegate tableView:self.unitsTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

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

#pragma mark UItableView Delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;//Changes 1
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return unitDataArray.count;//Changes 2
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"CellId";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[[unitDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];

return cell;
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.unitPicker reloadAllComponents];
}

#pragma mark UIPicker View Methods

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

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.row] objectForKey:@"units"];//Changes 4
return units.count;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSIndexPath *selectedIndexPath=[self.unitsTableView indexPathForSelectedRow];
NSArray *units=[[unitDataArray objectAtIndex:selectedIndexPath.row] objectForKey:@"units"];//Changes 5
return [units objectAtIndex:row];
}




@kết thúc

头文件也很简单。
#nhập 

@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet UIPickerView *unitPicker;
@property (weak,nonatomic) IBOutlet UITableView *unitsTableView;
@kết thúc

请查找 Demo project here

关于ios - UIPickerView 依赖于 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648459/

30 4 0
Bài viết được đề xuất: excel - 适用于 Excel 的 Apache POI : Setting the cell type to "text" for an entire column
Bài viết được đề xuất: java - 想要在HTML onclick(jsp)中调用Javabean方法
Bài viết được đề xuất: java - Hibernate 中空引用的查询处理
Bài viết được đề xuất: java加速图形
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com