簡體   English   中英

使用名稱和Int值填充的Swift Tableview單元格

[英]Swift Tableview Cells populated with name and a Int value

我正在嘗試構建我的第一個應用程序,但遇到了麻煩

我有一個視圖控制器,並且在這個視圖控制器內部是一個UITableView

現在,我已經能夠使用array.count填充此視圖控制器

當我點擊該項目以將其標記為已完成時,我已經能夠添加附件檢查標記

現在我的問題是我想點擊一個項目,將其標記為已完成,並在完成時將值寫在標簽上

每個單元格將具有不同的Int值

我在某處讀過這篇文章,所以我需要使用NSMutableArray,但是我不知道該如何使用

任何幫助將非常感激

這是代碼:

UTC 5月8日更新1215年

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中

NSMutableArrays與您在此處所做的相同。

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 object1: MyNewModel = MyNewModel("this is used", value: 1)
section1.append(object1)

就是說,如果您為模型創建了不錯的初始化方法,則可以輕松設置它。

現在,您可以使用此可變數組,以與執行操作相同的方式填充表格視圖,而不僅僅是將文本設置為數組中的該對象,而是將其設置為:

((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.cellForRowAtIndexPath() )並進行更改。

那是做事的錯誤方式。

您想要做的是擁有一個數組(可變數組,或Swift中的var數組),該數組記錄您能夠查看的當前狀態。 文本內容,整數值,選定狀態等

然后在cellForRowAtIndexPath ,使用數組中的數據配置單元格。

如果用戶點擊一個單元格,則獲取該單元格的數組條目,進行更改,然后告訴表視圖重繪該單元格(使用reloadRowsAtIndexPaths)。

我建議創建一個結構來存儲您的單元格的所有信息:

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

...

var tableArray: [CellData]

然后編寫您的cellForRowAtIndexPath方法以通過從結構數組中獲取這些值來配置單元。

您的代碼創建了3個不同的不可變數組section1,section2和section3,但是numberOfSectionsInTableView返回的固定值為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]
    ]
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM