繁体   English   中英

当在表视图中选择特定单元格时,如何使用另一个单元格扩展或替换单元格

[英]how to expand or replace the cell with another cell, when an particular cell select in table view

我已经在SO中问过这个疑问/问题了。 但没有得到解决方案。 请帮帮我....

我有一个表视图,将显示名称数据列表,直到10个数据。 但我需要的是,当用户按任何单元格时,该单元格应该替换为另一个单元格,其中包含一些图像,电话号码,相同的数据名称。 怎么做。

我有两个xib:1。normalcell 1. normalcell, 2. expandable/replace cell

这是我的viewconrolelr.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var Resultcount: UILabel!

    var tableData = ["thomas", "Alva", "Edition", "sath", "mallko", "techno park",... till 10 data]
    let cellSpacingHeight: CGFloat = 5

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var nib = UINib(nibName:"customCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")
        Resultcount.text = "\(tableData.count) Results"



    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
         return self.tableData.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return cellSpacingHeight
    }

    // Make the background color show through
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clearColor()
        return headerView
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        var cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell


        cell.vendorName.text = tableData[indexPath.section]

        return cell


    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

启动我的单元格将如下所示:

在此输入图像描述

当我按下那个单元格时,我需要做一些像这样的事情来替换下面的单元格:

在此输入图像描述

但是当我再次按下同一个细胞时,它应该再次进入正常细胞。

怎么做 ??

首先修改tableView:cellForRowAtIndexPath:实现如下。 然后,您需要实现单击处理程序。 一种方法是在MyCell类中。 另一种方法是覆盖selectRowAtIndexPath 如果不知道你想要什么(例如多重选择和单选),很难给出实际的代码,但这里有些东西。

BOOL clickedRows[MAX_ROWS]; // Init this array as all false in your init method. It would be better to use NSMutableArray or something similar...

// selectRowAtIndexPath code
int row = indexPath.row
if(clickedRows[row]) clickedRows[row]=NO; // we reverse the selection for the row
else clickedRows[row]=YES;
[self.tableView reloadData];

// cellForRowAt... code
MyCell *cell = [tableView dequeueResuableCell...
if(cell.clicked) { // Nice Nib
     [tableView registerNib:[UINib nibWithNibName... for CellReuse...
} else { // Grey Nib
     [tableView registerNib:[UINib nibWithNibName... for CellReuse...
}

您需要在xib上创建两个独立的单元格。 然后你可以使用check加载。你可以复制和粘贴它将完美地工作。 在cellForRow中这样:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if selectedIndexPath == indexPath && self.isExpand  == true{
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeaveBalanceExpandedCell", for: indexPath) as! LeaveBalanceExpandedCell
            cell.delegate = self
            return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "LeaveBalanceNormalCell", for: indexPath) as! LeaveBalanceNormalCell
            return  cell
        }
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//        cell.animateCell(cell)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndexPath == indexPath{
    if isExpand == true{
        self.isExpand = false
    }
    else{
        self.isExpand = true
    }
    }
    else{
        selectedIndexPath = indexPath
        self.isExpand = true
    }
        self.tableView.reloadData()
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM