簡體   English   中英

帶有UITextFields的UITableView部分

[英]UITableView section with UITextFields

我現在嘗試了很多天,以在部分單元格中設置和獲取UITextFields輸入。

在我的第一個視圖中,我進入了一個UITextField ,我在其中說了我想擁有多少個部分,按下按鈕時,它給了我UITableView中的這些部分(具有4行)。 我現在的問題是,我沒有獲得每個UITextField的正確值。

例如:

  • 所有部分都有4個文本字段
  • 在可以輸入int或float的文本字段中
  • 當我從文本字段1和2知道int時,我將它們除以...

這是我的代碼。

#import "ViewController.h"

@interface ViewController () 
{
    NSMutableArray *sectionsArray;
    NSMutableArray *rowsArray;
    NSMutableArray *textFieldArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.

    [[self myTableView]setDelegate:self];
    [[self myTableView]setDataSource:self];

    sectionsArray = [[NSMutableArray alloc]init];
    rowsArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];
    textFieldArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];

}

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData]; 
}


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

#pragma mark - Table View

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    int anzahl = [_AnzahlStationen intValue];//from first view...anzahl=quantity of sections

    //adds sections
    do {

        [sectionsArray addObject:[NSString stringWithFormat:@"Palette %lu",sectionsArray.count+1]];

    } while ([sectionsArray count] < anzahl);

    return sectionsArray.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return rowsArray.count;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return  [sectionsArray objectAtIndex:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 24)];
        textField.font = [UIFont fontWithName:@"Gujarati Sangam MN" size:15];
        textField.textColor = [UIColor redColor];

        textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        textField.tag = 17;
        [cell.contentView addSubview:textField];
        textField.delegate = self;
    }

    UITextField *textField = (UITextField *)[cell.contentView viewWithTag:17];

    textField.text = [textFieldArray objectAtIndex:indexPath.row];

    return cell; 
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; 
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

    [textFieldArray replaceObjectAtIndex:0 withObject:textField.text];
}

@end

如何在正確的行中替換正確的UITextField?

我建議您使用類似於以下的數據源:

NSMutableArray *tableViewData = [NSMutableArray array];

[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];

上面的代碼表示tableViewData ,其中包含4個數組,每個數組包含一個字典,該字典將維護單個單元格的數據。 在我的示例中,我只使用了UITextField的單個鍵值對。

您可以根據需要動態創建它,並且委托和數據源方法可以從中讀取。 而不是擁有3個單獨的數組。

例如

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return [tableViewData count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[tableViewData objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSDictionary *cellData = [[tableViewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    // Create your cell and use the data stored in the dictionary
}

通過使用上述方法,您可以更新此字典,並在單個位置維護所有UITableView的狀態,並且可以使代碼整潔和可讀。 要訪問任何UITextField ,只需在UITableView數據源和委托方法的字典中找到它即可。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM