我正在处理一个项目,以更改当段值更改时由表视图显示的array.实现这一目标的最简单方法是什么?谢谢!

  @IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
        if sender == testSwitcher {
            if testSwitcher.selectedSegmentIndex == 0 {
                print("Hello World")
            }
            else if testSwitcher.selectedSegmentIndex == 1 {
                print("Data Update")
                newArray = []
                stateElections.data = newArray
                stateElections.reloadData()
     }

推荐答案

您可以实现如下所示的内容

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
        
        switch(mySegmentedControl.selectedSegmentIndex)
        {
        case 0:
            myCell.textLabel!.text = arrayList1[indexPath.row]
            break
        case 1:
            myCell.textLabel!.text = arrayList2[indexPath.row]
            break
            
        case 2:
            myCell.textLabel!.text = arrayList3[indexPath.row]
            break
            
        default:
            break
            
        }


        return myCell
    }
    
    
    @IBAction func segmentedControlActionChanged(sender: AnyObject) {
    
        myTableView.reloadData()
    }

Swift相关问答推荐

Form中的Divider在macOS上并不完全扩展

MyApp类型不符合协议App''''

允许视图在视图内更改

如何从我的 macOS 应用程序打开 (.log) 文件?

RxSwift 提高性能

类型 '()' 不能符合 View (除非它肯定是 View,这次没有恶作剧)

`let case(value)` 和 `case(let value)` 之间的区别 (Swift)

用户输入以更改通过点击生成的形状大小

Swift UI 不重绘 NSViewRepresentable 包装的文本字段

如何从 LLDB 调用带有断点的 Swift 函数?

Apple 的自然语言 API 返回意外结果

Xcode swift ui 错误KeyPathComparator仅在 macOS 12.0 或更高版本中可用

通用枚举菜单 SwiftUI

组合 flatMap 不会返回预期的上下文结果类型

仅在 Swift 中创建 Setter

使用 swift 运行 pod install 时出错

关闭 UITableViewRowAction

使 struct 可散列?

UICollectionView 自定义单元格在 Swift 中填充宽度

显示 UIAlertController 的简单 App Delegate 方法(在 Swift 中)