【问题标题】:Adding textfield dynamically to table view cell and scroll it horizontally将文本字段动态添加到表格视图单元格并水平滚动
【发布时间】:2015-05-04 13:40:02
【问题描述】:

我创建了一个横向显示的表格视图。

我想向表格单元格动态添加多个文本字段,并且还想获取数据,任何人都可以建议我。如何在 iOS 中水平滚动?

【问题讨论】:

    标签: ios uitableview dynamic uiscrollview


    【解决方案1】:
       UITextField *mytextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
            mytextField.clearsOnBeginEditing = NO;
            mytextField.textAlignment = UITextAlignmentRight;
            mytextField.delegate = self;  
            [yourCell.contentView addSubview: mytextField];
    

    或

    这样试试

    - (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];
            }
    
           if (indexPath.row == 0){
         21)];
        self.mytextField1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
        self.mytextField1.placeholder = @"mytextField1";
        self.mytextField1.autocorrectionType = UITextAutocorrectionTypeNo;
        [self.mytextField1 setClearButtonMode:UITextFieldViewModeWhileEditing];
    
        [cell addSubview:self.mytextField1];
    }
    
    if (indexPath.row == 1){
        self.mytextField2 = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
        self.mytextField2.placeholder = @"mytextField2";
        self.mytextField2.autocorrectionType = UITextAutocorrectionTypeNo;
        [self.mytextField2 setClearButtonMode:UITextFieldViewModeWhileEditing];
    
        [cell addSubview:self.mytextField2];
    }
    
    if (indexPath.row == 2){
        // add here 
    }
    
    if (indexPath.row == 3){
    
    }
    
    self.mytextField1.delegate = self;
    self.mytextField2.delegate = self;
    // and other also here
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
            return cell;
        }
    

    【讨论】:

    • 我想在同一个单元格上添加所有文本字段而不是在不同的单元格上,然后水平滚动。如果我要输入 20-25 个文本字段,那么我如何动态生成它。
    • 为什么要在同一个单元格中添加?
    • 我的项目要求添加那些文本字段获取输入并以横向显示。并将运行时新行添加到表格视图
    • 或者在添加 UITextfield 的顶部使用 UIScrollview 和自定义 UIview 并添加你想要添加多少运行时提交的文本。
    • for (int i = indexPath.row ; i
    【解决方案2】:

    您可以先在 tableCell 上添加一个滚动视图,而不是在此滚动视图上添加多个 textField,而不是直接将 textField 添加到 tableCell 中。相应地设置滚动视图的内容大小,以便它可以轻松滚动到最后。

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多