如何在选择片段时从表格视图加载选定的数据?

时间:2018-04-17 08:47:54

标签: ios swift uitableview tableview uisegmentedcontrol

我想知道如何从我的表视图中加载选定的数据,例如,当选择段索引等于1时,表视图将重新加载,并且仅显示状态等于已批准的数据。因为您从我的下面的代码中看到,我已经加载了所有状态的所有数据:。例如,如果segmentView.selectedSegmentIndex == 1表将使用状态等于已批准的数据重新加载。我已经可以确定所选的索引了。我想要的是如何从表视图中访问这些数据,我可以根据状态

加载所选数据
.dragDiv {
  background-color: #14A07D;
  background: linear-gradient(#1BD6A7, #14A07D);
  background-clip: padding-box;
  display: table;
  text-align: center;
  width: 100%;
  height: 100%;
  position: absolute;
  overflow: hidden;
  z-index: 2;
  top: 0px;
  left: 0px;
  color: #FFFFFF;
  font-weight: bold;
  white-space: pre;
  border-radius: 2px;
}

.dragDiv:hover {
  cursor: -webkit-grab;
  cursor: -moz-grab;
  background: #ff0080;
  background: linear-gradient(#fe78ad, #ff0080);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  border-radius: 10px;
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  background-color: #FFFFFF;
}

table,
th,
td {
  border-bottom: 1px dashed #f0f0ec;
  border-top: 1px dashed #f0f0ec;
  border-right: 1px solid #e9e9e4;
  border-right: 1px solid #e9e9e4;
  *height: 16px;
  font-size: 12px;
  text-align: center;
}

td {
  position: relative;
}

3 个答案:

答案 0 :(得分:1)

最佳选择如下:

为展示制作一个共同的array,用于在tableView中显示数据:

并将其用于所有条件,例如:

制作一个像这样的通用函数:

   func filterDataStatusWise(strStatus:String){

    for dict in arrMainResponse{

        let strStatus = arrMainResponse["status"]

        if strStatus["name"] = strStatus{
            //Whatever data add in arrForDisplay here
        }

        tableView.reloadData()
    }
}

然后像这样使用它:

var arrForDisplay = [String:Any]()

if condition1{
        filterDataStatusWise(strStatus: "ongoing")
    }else if condition2{
        filterDataStatusWise(strStatus: "approved")
    }else if condition3{
        filterDataStatusWise(strStatus: "for approval")
    }else if condition4{
        filterDataStatusWise(strStatus: "near expiry")
    }else{
        filterDataStatusWise(strStatus: "expired")
    }

希望它有所帮助!

答案 1 :(得分:0)

根据所选索引(状态)过滤数组:

func tableView(tableView: UITableView, numberOfRowsInSection section: 
Int) -> Int {

    switch segmentView.selectedSegmentIndex {
    case 0:
        if self.expiredArray == nil {
            return 0
        }
        return (self.expiredArray?.count)!   
    case 1:
        if self.approvedArray == nil {
            return 0
        }
        return (self.complaintArray?.count)!
    default:
        break
    }
    return 0

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

    switch segmentView.selectedSegmentIndex {
    case 0:
    // Load from expired array
    case 1:
    // Load from approved array
    default:
        break
    }
    return UITableViewCell()
}

func selectSegmentInSegmentView(segmentView: SMSegmentView) {
   if segmentView.selectedSegmentIndex == 1 {
   // Write your code to filter array here based on status
   } else {
   // Write your code to filter array here based on status
   }
// reload tableview here.....

}

答案 2 :(得分:0)

您需要创建5个单独的数组来加载,同时选择不同的UISegmentControl段。

var ongoingArr = [[String: Any]]() // For segment index 0
var approvedArr = [[String: Any]]() // For segment index 1
var forApprovalArr = [[String: Any]]() // For segment index 2
var nearExpiryArr = [[String: Any]]() // For segment index 3
var expiredArr = [[String: Any]]() // For segment index 4

您拥有整个数据getAllDetail,并且您将通过API或之前的屏幕获取此数组中的数据:

var getAllDetail = [[String: Any]]()
  

如果您从API获取数据,则在getAllDetail中加载数据后加载5个以上的数组。只是创建数组的扩展名为:

extension Array where Element == [String: Any] {

    func filterArray(_ statusName: String) -> [Element] {
        return self.filter { infoDict -> Bool in
            if let statusDict = infoDict["status"] as? [String: String], let name = statusDict["name"] {
                return name == statusName
            }
            return false
        }
    }

}

并加载5个数组:

func loadSegmentArray() {

    ongoingArr = getAllDetail.filterArray("ongoing")
    approvedArr = getAllDetail.filterArray("approved")
    forApprovalArr = getAllDetail.filterArray("for approval")
    nearExpiryArr = getAllDetail.filterArray("near expiry")
    expiredArr = getAllDetail.filterArray("expired")

    // Select 0th index of segment and reload table
    segmentView.selectedSegmentIndex = 0
    self.setUpTableView() // Reload Table view
}

在您的func selectSegmentInSegmentView中,只需重新加载表格视图:

func selectSegmentInSegmentView(segmentView: SMSegmentView) {
    self.setUpTableView()
}
  

根据这5个阵列更新您的UITabelView委托和数据源方法。

     

对于示例,我正在撰写numberOfRowsInSectioncellForRowAt indexPath

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch segmentView.selectedSegmentIndex {
    case 0:
        return ongoingArr.count
    case 1:
        return approvedArr.count
    case 2:
        return forApprovalArr.count
    case 3:
        return nearExpiryArr.count
    case 4:
        return expiredArr.count
    default:
        return 0
    }
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "ToDoListTableViewCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ToDoListTableViewCell
    cell.delegate = self

    var infoDict = [String: Any]()
    switch segmentView.selectedSegmentIndex {
    case 0:
        infoDict = ongoingArr[indexPath.row]
        cell.statuslabel.backgroundColor = // Color of onging
    case 1:
        infoDict = approvedArr[indexPath.row]
        cell.statuslabel.backgroundColor = // Color of approvedArr
    case 2:
        infoDict = forApprovalArr[indexPath.row]
        cell.statuslabel.backgroundColor = // Color of forApprovalArr
    case 3:
        infoDict = nearExpiryArr[indexPath.row]
        cell.statuslabel.backgroundColor = // Color of nearExpiryArr
    case 4:
        infoDict = expiredArr[indexPath.row]
        cell.statuslabel.backgroundColor = // Color of expiredArr
    default:
        cell.statuslabel.backgroundColor = .black
    }

    cell.toDoItemLabel.text = infoDict["name"] as? String
    return cell
}