Ios 用名称和Int值填充的Swift Tableview单元格

Ios 用名称和Int值填充的Swift Tableview单元格,ios,uitableview,swift,nsmutablearray,Ios,Uitableview,Swift,Nsmutablearray,我正在尝试构建我的第一个应用程序,但我遇到了麻烦 我有一个视图控制器,该视图控制器内有一个UITableView 现在,我已经能够使用array.count填充此视图控制器 当我点击项目以标记sa已完成时,我已经能够添加一个accesory复选标记 现在我的问题是,我想点击一个项目,将其标记为已完成,并在完成时在标签上写入一个值 每个单元格都有不同的Int值 我在某个地方读到,要这样做,我需要使用NSMutableArray,但我不知道如何使用它 任何帮助都将不胜感激 代码如下: 协调世界时12

我正在尝试构建我的第一个应用程序,但我遇到了麻烦

我有一个视图控制器,该视图控制器内有一个UITableView

现在,我已经能够使用array.count填充此视图控制器

当我点击项目以标记sa已完成时,我已经能够添加一个accesory复选标记

现在我的问题是,我想点击一个项目,将其标记为已完成,并在完成时在标签上写入一个值

每个单元格都有不同的Int值

我在某个地方读到,要这样做,我需要使用NSMutableArray,但我不知道如何使用它

任何帮助都将不胜感激

代码如下:

协调世界时1215年5月8日更新

import UIKit

// --------------------------------------------------------------------------------------------------------------------------\
class ViewController: UIViewController, UITableViewDataSource {                        //class and subclass                  |)
//---------------------------------------------------------------------------------------------------------------------------/
    // Variable and constant, also IBAOutlet


    struct CellData
    {
        var title: String
        var value: String
        var selected: Bool

    }

    var CellData1 = CellData(title: "this is a test", value: "1", selected: true)
    var CellData2 = CellData(title: "this is a test", value: "1", selected: true)
    var CellData3 = CellData(title: "this is a test", value: "1", selected: true)
    var CellData4 = CellData(title: "this is a test", value: "1", selected: true)

    var tableArray: [CellData] = []

    @IBOutlet weak var scoreshow: UILabel!
    @IBOutlet weak var tableView: UITableView!

// --------------------------------------------------------------------------------------

    override func viewDidLoad() {

        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
    }
//----------------------------------------------------------------------------------------
    // checkmarks when tapped

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            if cell.accessoryType == .Checkmark
            {
                cell.accessoryType = .None
                cell.backgroundColor = UIColor.whiteColor()
            }
            else
            {
                cell.accessoryType = .Checkmark
                cell.backgroundColor = UIColor.yellowColor()            }
        }    
    }

//----------------------------------------------------------------------------------------
    //number of sections for the table

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
//----------------------------------------------------------------------------------------
    //Calculate the amount of rows

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.tableArray.count;
    }
//----------------------------------------------------------------------------------------
    //Cells text label and config

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


        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
        cell.textLabel!.text = (tableArray[indexPath.row]).title
        scoreshow.text = (tableArray[indexPath.row]).value
        cell.textLabel!.numberOfLines = 0
        cell.selectionStyle = UITableViewCellSelectionStyle.None;

        return cell
 }

//----------------------------------------------------------------------------------------
    //resetbutton

    @IBAction func resetcheck(sender: UIButton) {

            for i in 0...tableView.numberOfSections()-1
            {
                for j in 0...tableView.numberOfRowsInSection(i)-1
                {
                    if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) {
                        cell.accessoryType = .None
                        cell.backgroundColor = UIColor.whiteColor()
                    }

                }
            }
        }

//----------------------------------------------------------------------------------------

因此,首先您可能应该创建NSObject的一个新子类,并对其进行设置,使其接受objectText和objectValue等属性

然后创建任意数量的对象,并将其存储在NSMutableArray中

NSMutableArray与您在此处所做的相同

let section1 =
    ["this is used",
     "this is used to test",
     "this is used to test the lenght",
     "this is used to test the lenght of the text",
     "this is used to test the lenght of the text",
     "this is used to test the lenght of the text",
     "this is used to test the lenght of the text",
     "this is used to test the lenght of the text",
     "this is used to test the lenght of the text",]
有点。。。除非您希望将其设置为var,而不是let,以便可以修改它

你会得到这样的结果:

let object1: MyNewModel = MyNewModel("this is used", value: 1)
section1.append(object1)
如果您为您的模型创建了一个合适的init方法,那么您就可以轻松地进行设置

现在,您可以使用此可变数组以与当前相同的方式填充tableview,但不只是将文本设置为数组中的该对象,而是将其设置为:

((MyNewModel)section1[indexPath.row]).objectText
接下来要做的事情是子类UITableViewCell,并在单元格中放置一些标签,您希望用这些模型中的文本填充这些标签。因此,为他们提供一些公共属性以访问表格单元格内的标签,替换此行:

let cell:UITableViewCell =
UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
为此:

let cell = MyNewTableCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
然后,您可以访问单元格内的新标签,并将其设置为:

cell.myTextLabel!.text = ((MyNewModel)section1[indexPath.row]).objectText
cell.myValueLabel!.text = ((MyNewModel)section1[indexPath.row]).objectValue

无论何时需要更改方法,您都可以隐藏或显示任何想要显示的内容

当用户对单元格执行某些操作(如选择)时,您正在抓取该单元格(使用
tableView.cellforrowatinexpath()
)并对其进行更改

那是错误的做事方式

您要做的是创建一个数组(可变数组,或Swift中的
var
数组),用于记录可编辑视图的当前状态。文本内容、整数值、选定状态等

然后在
单元格中,使用数组中的数据配置单元格

如果用户点击某个单元格,则获取该单元格的数组项,对其进行更改,然后告诉表视图重新绘制该单元格(使用reloadrowsatindexpath)

我建议创建一个结构来存储单元格的所有信息:

struct CellData
{
  var title: String
  var value: Int
  var selected: Bool
}

然后编写
cellforrowatinexpath
方法,通过从结构数组中获取这些值来配置单元格

您的代码创建了3个不同的不可变数组section1、section2和section3,但NumberOfSectionsTableView返回固定值5,
cellForRowAtIndexPath
始终返回从section1生成的单元格。那太离谱了

如果您只是在玩,请暂时跳过分区表视图。它会把你弄糊涂的。从一个单独的分区表视图开始,掌握其中的诀窍,然后在以后处理分区表视图

准备好剖切表视图后,请尝试使用数组数组:

//The array for a section is an array of CellData
typealias sectionArray: [CellData] 

//The array for the whole table is an array of sectionArray arrays.
var tableArray: [sectionArray]

init()
{
  tableArray = [ 
    sectionArray[
      CellData(title:"section 0, row 0", value: 0, selected: false],
      CellData(title:"section 0, row 1", value: 1, selected: false],
      CellData(title:"section 0, row 2", value: 2, selected: false]
    ],
    sectionArray[
      CellData(title:"section 1, row 0", value: 10, selected: false],
      CellData(title:"section 2, row 1", value: 11, selected: false],
      CellData(title:"section 3, row 2", value: 12, selected: false]
    ]
  ]
}

非常感谢您提供的详细信息,我现在将尝试,对于多个数组,我之所以这样做有两个原因:第一个是测试行的长度和数量,第二个是因为最终当应用程序正常运行时,我希望将我的数据分为5个部分(页眉在页脚中)现在,我应该继续使用剖视图,还是从1数组开始,然后再从1数组开始工作?谢谢你的回答,但我看不出这在我的项目中有多合适。它适合你的项目,因为你应该使用表视图。直接操纵单元格是错误的,会给你带来各种各样的问题。好吧,我已经尝试将其纳入到项目中,这意味着我的重置按钮、单元格填充方法和触摸完成方法将过时,老实说,我的方向是对的,但是我已经花了很多时间来完成这三件事,我在swift方面的知识太少,我无法从头开始,我会继续努力,只是很难把这三件事放在一起。这是一个错误,它会回来咬你。毫无疑问,你目前的做法是绝对错误的。想象一下:在表视图中有30个条目。用户选择不同的单元格,并在前几个单元格中输入不同的数据。然后用户向下滚动10或15个单元格。最上面的单元格会从屏幕上滚动,它们的数据将永远丢失。当用户滚动回表格视图的顶部时,单元格将显示其旧值,更改将丢失。感谢您的回答,我是swift新手,因此仅为此创建int对我来说很难,我将继续努力,并让您知道我是否有任何进展。我完全停留在开始阶段,无法理解init()与代码一起工作的方法
当您创建一个新类,NSObject的子类时,您可以像普通方法一样创建一个init方法,如下所示:
类TestObject:NSObject{init(objectTitle:String,objectValue:Int){super.init()                       }      }
注释上的格式设置有点糟糕,或者如果使用init方法,然后将值设置到作为类属性的变量上,那么您应该能够从TableView访问这些属性。更新的代码没有意义。CellData有两种不同的定义:一种是类,另一种是结构。您的类在我看来,定义创建了一个无限递归崩溃(每个CellData对象)
//The array for a section is an array of CellData
typealias sectionArray: [CellData] 

//The array for the whole table is an array of sectionArray arrays.
var tableArray: [sectionArray]

init()
{
  tableArray = [ 
    sectionArray[
      CellData(title:"section 0, row 0", value: 0, selected: false],
      CellData(title:"section 0, row 1", value: 1, selected: false],
      CellData(title:"section 0, row 2", value: 2, selected: false]
    ],
    sectionArray[
      CellData(title:"section 1, row 0", value: 10, selected: false],
      CellData(title:"section 2, row 1", value: 11, selected: false],
      CellData(title:"section 3, row 2", value: 12, selected: false]
    ]
  ]
}