如何根据从数据库中提取的数据将UITableView中正确的自定义单元格出列?

时间:2016-12-11 00:25:09

标签: ios swift uitableview

我有一个UITableView,其中包含两个自定义UITableViewCell类,我会在表视图中显示这些类。

我的故事板看起来像这样:

enter image description here

以前,我在数据中进行了硬编码,并基于UITableView indexPath.row,我会将相应的自定义单元格类出列并加载数据。

一个例子:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    switch (indexPath.row)
    {
        case 0...9:

            let 3Pic = tableView.dequeueReusableCell(withIdentifier: "3PicRow", for: indexPath) as! 3PicRowCell

            if indexPath.row == 0
            {
                3Pic.leftNameLabel.text = "Name"
                3Pic.leftPositionLabel.text = "Position"
                3Pic.leftImageView.image = UIImage(named: "icon")

                3Pic.MidddleNameLabel.text = "Name"
                3Pic.MiddlePositionLabel.text = "Position"
                3Pic.MiddleImageView.image = UIImage(named: "icon")

                3Pic.RightNameLabel.text = "Name"
                3Pic.RightPositionLabel.text = "Position"
                3Pic.RightImageView.image = UIImage(named: "icon")
            }
            else if indexPath.row == 1 // row=1 through row=9
            {
                // Do same thing
            }
            return 3Pic

        case 10...12:
            let 2Pic = tableView.dequeueReusableCell(withIdentifier: "2PicRow", for: indexPath) as! 2PicRowCell

            if indexPath.row == 10
            {
                2Pic.leftNameLabel.text = "Name"
                2Pic.leftPositionLabel.text = "Position"
                2Pic.leftImageView.image = UIImage(named: "icon_about")

                2Pic.rightNameLabel.text = "Name"
                2Pic.rightPositionLabel.text = "Position"
                2Pic.rightImageView.image = UIImage(named: "icon")
            }

            // Do same thing until row=12

            return 2Pic
        default:
            break;
    }

    // Will never reach here
    return UITableViewCell()
}

既然我们正在迁移到严格从数据库中提取的数据,那么我很难弄清楚如何根据提取的数据量来确定合适的单元格。

JSON数据示例:

{
    "item1": {
        "id": 1,
        "first_name": "First",
        "last_name": "Last",
        "position": "Position",
        "image_url": "icon.com"
    },
    "item2": {
        "id": 2,
        "first_name": "First",
        "last_name": "Last",
        "position": "Position",
        "image_url": "icon.com"
    },
    "item3": {
        "id": 3,
        "first_name": "First",
        "last_name": "Last",
        "position": "Position",
        "image_url": "icon.com"
    }
}

任何人都可以为我的困境提供解决方案吗?

2 个答案:

答案 0 :(得分:0)

您可以检查def save(sales, taken_seats): #code here to save sales and taken seats to the file. f = open("data.txt", "w") f.write(str(sales) + "\n") for x in taken_seats: f.write(x + "\n") f.close() def load(): #code here to load the sales total and currently purchased seats. f = open("data.txt") sales = f.readline().replace("\n", "") taken_seats = [] d = True while d == True: line = f.readline().replace("\n", "") if line == "": d = False else: taken_seats.append(line) if sales == "": sales = 0 else: sales = int(sales) f.close() return (int(sales), taken_seats) def display_seats(taken_seats): seating = [] for xd in range(15): row = [] xda = 0 for xda in range(15): row.append("#") seating.append(row) for x in taken_seats: pos = x.split(",") seating[(int(pos[0]) - 1)][(int(pos[1]))] = "X" dx = 1 for row in seating: if len(str(dx)) < 2: de = " " + str(dx) else: de = dx print ("Row: " + str(de) + " ".join(row)) dx = dx + 1 def list_options(): print ("5: exit") print ("4: buy ticket") print ("3: view sales") print ("2: view price per row") print ("1: view current seating") new_input = input("Your choice: ") return (new_input) def purchase_seat(taken_seats): print ("Would you like to view current seating availability? ") print ("'1' = yes, '2' = no") newinput = input("? ") if newinput == "1": display_seats(taken_seats) x = True while x == True: cost = 0 print ("what row would you like to buy a seat on? ") rowx = input("What row? ") print ("What seat would you like to purchase?") rowy = input("what seat? ") d = (str(rowx) + "," + str(rowy)) h = 0 for x in taken_seats: if d == x: h = 1 if h == 1: print ("That seat is already taken, please choose another seat.") elif int(rowx) > 15 or int(rowy) > 15: print ("Invalid seating location, please choose another seat.") else: print ("seat purchased.") cost = (200 - (10 * int(rowx))) x = False return (cost, (str(rowx) + "," + str(rowy))) da = load() sales = da[0] taken_seats = da[1] quitter = 0 while quitter == 0: new_input = list_options() if new_input == "5": save(sales, taken_seats) quitter = 1 elif new_input == "4": g = True while g == True: new_seat = purchase_seat(taken_seats) taken_seats.append(new_seat[1]) print ("That will be: $ " + str(new_seat[0])) sales = sales + new_seat[0] print ("Would you like to purchase another seat?") new_input = input("'1' = yes, '2' = no: ") if new_input == "1": pass else: g = False elif new_input == "3": print ("Total sales: $" + str(sales)) elif new_input == "2": xd = 0 while xd < 15: print ("Row " + str(xd + 1) + ": is $" + str((200 - (10 * xd)) - 10)) xd = xd + 1 elif new_input == "1": display_seats(taken_seats) else: print ("invalid option.") JSON数据的密钥计数。如果您有3个密钥,请使用row单元格。如果您有2个密钥,请使用3pic单元格。

伪代码看起来有点像:

2pic

我认为使用集合视图会更简洁。你只需要1个单元格。

答案 1 :(得分:0)

您不需要第二个tableview单元格。你应该使用UIStackView。每个图像和标签都是垂直堆栈视图,所有三个都在水平堆栈视图中。当您没有要显示的信息时,只需隐藏中间垂直堆栈视图,水平堆栈视图将重新分配其他2个堆栈视图以取代它。