How to make UITableview with Textfield in swift?

前端 未结 4 541
广开言路
广开言路 2020-12-15 21:30

I want to make a table view with textfields in each cell,

I have a custom class in a swift file:

import UIKit

public class TextInputTableViewCell: U         


        
4条回答
  •  我在风中等你
    2020-12-15 22:15

    So here is a way to do it using the suggestion of having a textfield array. It uses the tag of the textfield to ensure it does not get added multiple times when you scroll.

    // Assumes that you have a label and a textfield as a part of you tableViewCell
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? BodyStatsTableViewCell else {
            return UITableViewCell()
        }
        cell.rowLabel.text = bodyTableArray[indexPath.row].rowTitle
        let tagValue = indexPath.row + 100
        var newRow = true
        for textvalue in arrayTextField{
            if textvalue.tag == tagValue {
                newRow = false
            }
        }
        if newRow {
            cell.rowTextField.tag = tagValue
            self.arrayTextField.append(cell.rowTextField)
            cell.rowTextField.text = bodyTableArray[indexPath.row].rowValue
        }
        return cell
    }
    

    // And then you cam get the values when you want to save with where tag - 100 will be the array index value to update

    for textvalue in arrayTextField{ print(textvalue.text) }

提交回复
热议问题