如何通过点击该节的单元格中插入新行:我想要像iPhone这样的布局,例如iPhone contact contact swift uitablview

发布于 2025-02-01 10:41:25 字数 10531 浏览 9 评论 0原文

blockquote

在此处输入图像描述 这就是我到目前为止所取得的成就,我的最终目标是设计如下图所共享的图像。我想提到的一件事是,当我单击移动单元格并在其上方插入一个新的“自定义行”时,它可以很好地

工作i.sstatic.net/dy6ti.jpg“ rel =“ nofollow noreferrer”>在此处输入图像描述 只要查看图片,这就是我要在单击添加电子邮件单元时要实现的目标,我可以将新行插入该部分,当我单击“添加手机”时,我可以在该单元格上方插入新行

         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                _ = indexPath.row
                // print(indexPath)
                //myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
               myTableView.deselectRow(at: indexPath, animated: true)
                if(indexPath.section == 1) {
                   // print(indexPath)
                    print("hello")
                    myTableView.setEditing(true, animated: true)
                    _ = setEditing(true, animated: true)
                   // myTableView.deselectRow(at: indexPath, animated: true)
                   //phone.insert("Company", at: indexPath.row)
                     //   myTableView.beginUpdates()
                        //  _ = tableView.dequeueReusableCell(withIdentifier: //"ContactMenuTableViewCell", for: indexPath) as! ContactMenuTableViewCell
                                  
                              //  myTableView.insertRows(at: [IndexPath(row: 0, section: 1)], with: .automatic)
                              //myTableView.endUpdates()
                    }
               else if(indexPath.section == 2){
                    print("oye hoye")
                    myTableView.setEditing(true, animated: true)
                    print("maan bhi jao")
                _ = setEditing(true, animated: true)
              
                }
               else if(indexPath.section == 3){
               
                  myTableView.setEditing(true, animated: true)
                  _ = setEditing(true, animated: true)
                     // myTableView.deselectRow(at: indexPath, animated: true)
                  //  if selectedIndexPath == indexPath.row {
                   /* address.insert(" new country", at: indexPath.row)
                         myTableView.beginUpdates()
                        //  let datePickerCell = Bundle.main.loadNibNamed("PhoneTableViewCell", owner: self, options: nil)?.first as! PhoneTableViewCell
                     myTableView.insertRows(at: [IndexPath(row: 0, section: 3)], with: .automatic)
                         myTableView.endUpdates()
                     */
                    }
        
              //set editing function  used to perform editing on cells
            }override func setEditing(_ editing: Bool, animated: Bool) {
            super.setEditing(editing, animated: animated)
            if editing{
                    print("12345")
                if isEditing{
                    self.myTableView.setEditing(true, animated: true)
                      myTableView.beginUpdates()
                      myTableView.insertRows(at: [IndexPath(row: phone.count ,section: 1)], with: .automatic)
                    myTableView.endUpdates()}
                print("missing u ")
            } else if editing  {
                    print("heloo guys how u  doing g")
                    self.myTableView.setEditing(true, animated: true)
                      myTableView.beginUpdates()
                      myTableView.insertRows(at: [IndexPath(row: email.count ,section: 2)], with: .automatic)
                              myTableView.endUpdates()
                } else if editing {
                    print("hurra g")
                   // self.myTableView.setEditing(true, animated: true)
                      myTableView.beginUpdates()
                      myTableView.insertRows(at: [IndexPath(row: address.count ,section: 3)], with: .automatic)
                              myTableView.endUpdates()
                }
                    
                else{
                    self.myTableView.setEditing(false, animated: true)
                    myTableView.beginUpdates()
                    self.myTableView.deleteRows(at: [IndexPath(row:phone.count, section: 1)], with: .automatic)
                    self.myTableView.endUpdates()
                }
            } 
        // editing style function
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
               
               if editingStyle == .delete{
                   self.fields.remove(at: indexPath.row)
                   self.myTableView.beginUpdates()
                       myTableView.deleteRows(at :[IndexPath(row : 0 , section: 0)], with: .fade)
                       self.myTableView.endUpdates()
                   
               }else if editingStyle == .insert {
                    if indexPath.section == 1{
                        print("i am new")
                    self.myTableView.beginUpdates()
                    self.phone.insert("company", at: 1)
                        myTableView.insertRows(at: [IndexPath(row:0, section: 1)], with: .automatic)
                        self.myTableView.endUpdates()
                    } else if indexPath.section == 2{
                        print("dekho")
                        self.myTableView.beginUpdates()
                        self.email.insert("home", at: 0)
                        myTableView.insertRows(at: [IndexPath(row: 0, section: 2)], with: .automatic)
                        self.myTableView.endUpdates()
                    }
                    else if indexPath.section == 3{
                        self.myTableView.beginUpdates()
                        self.address.insert("home", at: 0)
                        myTableView.insertRows(at: [IndexPath(row: 0, section: 3)], with: .automatic)
                        self.myTableView.endUpdates()
                    }
                }
        
               
                } 
        // different section with custom cells
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                switch indexPath.section{
                case 0:
                    let TextCell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
                                     TextCell.placeHolder.placeholder = fields[indexPath.row]
                                     TextCell.selectionStyle = .none
                                         return TextCell
                case 1:
                    if  indexPath.row == self.phone.count && self.isEditing == true{
                                  print(indexPath.row)
                                let hellocell  = tableView.dequeueReusableCell(withIdentifier: "ContactMenuTableViewCell", for: indexPath) as! ContactMenuTableViewCell
                               hellocell.newcontact.text = "Insert a new cell"
                                  return hellocell
                               }
                     let phoneCell = tableView.dequeueReusableCell(withIdentifier: "PhoneTableViewCell", for: indexPath) as! PhoneTableViewCell
                               // phoneCell.configure(with: "Add Phone", imageName: "gear")
                 phoneCell.addPhone.text = phone[indexPath.row]
                                return phoneCell
               case 2:
                if  indexPath.row == self.email.count && self.isEditing == true{
                   print(indexPath.row)
                 let insertCell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
                 //insertCell.emailInsert.placeholder = fields[indexPath.row]
                                             insertCell.selectionStyle = .none
                                                 return insertCell
                }
               if  indexPath.row == self.email.count && self.isEditing == true{
                           print(indexPath.row)
                         let hellocell  = tableView.dequeueReusableCell(withIdentifier: "ContactMenuTableViewCell", for: indexPath) as! ContactMenuTableViewCell
                        hellocell.newcontact.text = "Insert a new cell"
                           return hellocell
                        }
                    let emailCell = tableView.dequeueReusableCell(withIdentifier: "EmailTableViewCell", for: indexPath) as! EmailTableViewCell
                emailCell.emaillabel.text = email[indexPath.row]
                    return emailCell
                case 3:
                    
                   let addressCell = tableView.dequeueReusableCell(withIdentifier: "AddressTableViewCell", for: indexPath) as! AddressTableViewCell
                   addressCell.addressLabel.text = address[indexPath.row]
                   return addressCell
                default:
                    return UITableViewCell()
                }
            
           // number of rows function
         func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 1{
                if self.isEditing{
                     return phone.count + 1
                }else{
                    return phone.count
                }
            }  else if section == 2{
                if self.isEditing{
                     return email.count + 1
                }else{
                    return email.count
                }
            } else if section == 3{
                return address.count
            }else{
               return  fields.count
            }
        
            }
    
    // how to insert new row into specific section of tableview by tapping on existing cell of that section
    // also how to disable set editing function on other section while inserting row 
    // how to achieve insert cell functionality
    // i am designing a layout of edit contact like your iOS contact screen 
    // if anybody have code please help me out
// any help would be appreciated
//tableview cells
//inserting new rows 
// swipe to delete row
//swift
// uitableview

, UITATIONVIEWCELL对象是管理单个表行的内容的专用视图。您主要使用单元格来组织和展示应用程序的自定义内容,但是UitaiteViewCell提供了一些特定的自定义来支持与桌子相关的行为,包括: 将选择或突出显示在单元格上。 添加标准配件视图,例如细节或披露控制。 将细胞置于一个可编辑状态。 缩进单元格的内容以在表中创建视觉层次结构。 您的应用程序的内容占据了大部分单元格的边界,但是单元格可能会调整该空间以腾出其他内容。单元格在其内容区域的后缘上显示辅助视图。当您将表格放入编辑模式时,单元格会在其内容区域的前沿添加删除控件,并选择将附件视图换成重新订购控件。

Blockquote

enter image description here
this is what I have achieved so far in this regard and my ultimate goal is to design screen like image shared below. one thing I would like to mention is that it works well when I click on mobile cell and it inserts a new row custom row above it but whenever I add code for below too fields it throws an exception

enter image description here
just have a look at picture this is what I want to achieve whenever I click on add email cell i could insert new row into that section and when I click on add phone I can insert new row above that cell which is of phone type

         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                _ = indexPath.row
                // print(indexPath)
                //myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
               myTableView.deselectRow(at: indexPath, animated: true)
                if(indexPath.section == 1) {
                   // print(indexPath)
                    print("hello")
                    myTableView.setEditing(true, animated: true)
                    _ = setEditing(true, animated: true)
                   // myTableView.deselectRow(at: indexPath, animated: true)
                   //phone.insert("Company", at: indexPath.row)
                     //   myTableView.beginUpdates()
                        //  _ = tableView.dequeueReusableCell(withIdentifier: //"ContactMenuTableViewCell", for: indexPath) as! ContactMenuTableViewCell
                                  
                              //  myTableView.insertRows(at: [IndexPath(row: 0, section: 1)], with: .automatic)
                              //myTableView.endUpdates()
                    }
               else if(indexPath.section == 2){
                    print("oye hoye")
                    myTableView.setEditing(true, animated: true)
                    print("maan bhi jao")
                _ = setEditing(true, animated: true)
              
                }
               else if(indexPath.section == 3){
               
                  myTableView.setEditing(true, animated: true)
                  _ = setEditing(true, animated: true)
                     // myTableView.deselectRow(at: indexPath, animated: true)
                  //  if selectedIndexPath == indexPath.row {
                   /* address.insert(" new country", at: indexPath.row)
                         myTableView.beginUpdates()
                        //  let datePickerCell = Bundle.main.loadNibNamed("PhoneTableViewCell", owner: self, options: nil)?.first as! PhoneTableViewCell
                     myTableView.insertRows(at: [IndexPath(row: 0, section: 3)], with: .automatic)
                         myTableView.endUpdates()
                     */
                    }
        
              //set editing function  used to perform editing on cells
            }override func setEditing(_ editing: Bool, animated: Bool) {
            super.setEditing(editing, animated: animated)
            if editing{
                    print("12345")
                if isEditing{
                    self.myTableView.setEditing(true, animated: true)
                      myTableView.beginUpdates()
                      myTableView.insertRows(at: [IndexPath(row: phone.count ,section: 1)], with: .automatic)
                    myTableView.endUpdates()}
                print("missing u ")
            } else if editing  {
                    print("heloo guys how u  doing g")
                    self.myTableView.setEditing(true, animated: true)
                      myTableView.beginUpdates()
                      myTableView.insertRows(at: [IndexPath(row: email.count ,section: 2)], with: .automatic)
                              myTableView.endUpdates()
                } else if editing {
                    print("hurra g")
                   // self.myTableView.setEditing(true, animated: true)
                      myTableView.beginUpdates()
                      myTableView.insertRows(at: [IndexPath(row: address.count ,section: 3)], with: .automatic)
                              myTableView.endUpdates()
                }
                    
                else{
                    self.myTableView.setEditing(false, animated: true)
                    myTableView.beginUpdates()
                    self.myTableView.deleteRows(at: [IndexPath(row:phone.count, section: 1)], with: .automatic)
                    self.myTableView.endUpdates()
                }
            } 
        // editing style function
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
               
               if editingStyle == .delete{
                   self.fields.remove(at: indexPath.row)
                   self.myTableView.beginUpdates()
                       myTableView.deleteRows(at :[IndexPath(row : 0 , section: 0)], with: .fade)
                       self.myTableView.endUpdates()
                   
               }else if editingStyle == .insert {
                    if indexPath.section == 1{
                        print("i am new")
                    self.myTableView.beginUpdates()
                    self.phone.insert("company", at: 1)
                        myTableView.insertRows(at: [IndexPath(row:0, section: 1)], with: .automatic)
                        self.myTableView.endUpdates()
                    } else if indexPath.section == 2{
                        print("dekho")
                        self.myTableView.beginUpdates()
                        self.email.insert("home", at: 0)
                        myTableView.insertRows(at: [IndexPath(row: 0, section: 2)], with: .automatic)
                        self.myTableView.endUpdates()
                    }
                    else if indexPath.section == 3{
                        self.myTableView.beginUpdates()
                        self.address.insert("home", at: 0)
                        myTableView.insertRows(at: [IndexPath(row: 0, section: 3)], with: .automatic)
                        self.myTableView.endUpdates()
                    }
                }
        
               
                } 
        // different section with custom cells
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                switch indexPath.section{
                case 0:
                    let TextCell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
                                     TextCell.placeHolder.placeholder = fields[indexPath.row]
                                     TextCell.selectionStyle = .none
                                         return TextCell
                case 1:
                    if  indexPath.row == self.phone.count && self.isEditing == true{
                                  print(indexPath.row)
                                let hellocell  = tableView.dequeueReusableCell(withIdentifier: "ContactMenuTableViewCell", for: indexPath) as! ContactMenuTableViewCell
                               hellocell.newcontact.text = "Insert a new cell"
                                  return hellocell
                               }
                     let phoneCell = tableView.dequeueReusableCell(withIdentifier: "PhoneTableViewCell", for: indexPath) as! PhoneTableViewCell
                               // phoneCell.configure(with: "Add Phone", imageName: "gear")
                 phoneCell.addPhone.text = phone[indexPath.row]
                                return phoneCell
               case 2:
                if  indexPath.row == self.email.count && self.isEditing == true{
                   print(indexPath.row)
                 let insertCell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
                 //insertCell.emailInsert.placeholder = fields[indexPath.row]
                                             insertCell.selectionStyle = .none
                                                 return insertCell
                }
               if  indexPath.row == self.email.count && self.isEditing == true{
                           print(indexPath.row)
                         let hellocell  = tableView.dequeueReusableCell(withIdentifier: "ContactMenuTableViewCell", for: indexPath) as! ContactMenuTableViewCell
                        hellocell.newcontact.text = "Insert a new cell"
                           return hellocell
                        }
                    let emailCell = tableView.dequeueReusableCell(withIdentifier: "EmailTableViewCell", for: indexPath) as! EmailTableViewCell
                emailCell.emaillabel.text = email[indexPath.row]
                    return emailCell
                case 3:
                    
                   let addressCell = tableView.dequeueReusableCell(withIdentifier: "AddressTableViewCell", for: indexPath) as! AddressTableViewCell
                   addressCell.addressLabel.text = address[indexPath.row]
                   return addressCell
                default:
                    return UITableViewCell()
                }
            
           // number of rows function
         func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 1{
                if self.isEditing{
                     return phone.count + 1
                }else{
                    return phone.count
                }
            }  else if section == 2{
                if self.isEditing{
                     return email.count + 1
                }else{
                    return email.count
                }
            } else if section == 3{
                return address.count
            }else{
               return  fields.count
            }
        
            }
    
    // how to insert new row into specific section of tableview by tapping on existing cell of that section
    // also how to disable set editing function on other section while inserting row 
    // how to achieve insert cell functionality
    // i am designing a layout of edit contact like your iOS contact screen 
    // if anybody have code please help me out
// any help would be appreciated
//tableview cells
//inserting new rows 
// swipe to delete row
//swift
// uitableview

//A UITableViewCell object is a specialized type of view that manages the content of a single table row. You use cells primarily to organize and present your app’s custom content, but UITableViewCell provides some specific customizations to support table-related behaviors, including:
Applying a selection or highlight color to the cell.
Adding standard accessory views, such as a detail or disclosure control.
Putting the cell into an editable state.
Indenting the cell's content to create a visual hierarchy in your table.
Your app’s content occupies most of the cell’s bounds, but the cell may adjust that space to make room for other content. Cells display accessory views on the trailing edge of their content area. When you put your table into edit mode, the cell adds a delete control to the leading edge of its content area, and optionally swaps out an accessory view for a reorder control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文