在表格视图中仅显示第一个单元格

时间:2018-02-23 21:21:00

标签: ios swift uitableview core-data

我在视图控制器的地图视图顶部使用了一个表视图和表视图单元格,我只想显示第一个单元格。我已经尝试将numberOfRowsInSection设置为1,这会导致我的应用崩溃。

填充单元格并确定部分中的行数的数据来自Core Data。我已经成功地只显示了我想要的数据,它只是重复了一堆,而不只是显示一个单元格。

我想要这个,就像我在单独的Table View Controller中一样:

enter image description here

相反,我明白了:

enter image description here

我的cellForRowAt:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Configure cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "MapTableViewCell") as! MapTableViewCell
    cell.thumbnailImageView.layer.cornerRadius = 10
    cell.horizontalStackView.addBackground(color: UIColor.white)

    // Get data
    var cellLocation: Location

    for location in locations {
        if location.latitude == annotation.coordinate.latitude && location.longitude == annotation.coordinate.longitude {
            cellLocation = location

    YelpClient.sharedInstance().loadImage(cellLocation.imageUrl, completionHandler: { (image) in

        performUIUpdatesOnMain {

            cell.thumbnailImageView.layer.cornerRadius = 10
            cell.thumbnailImageView.clipsToBounds = true
            cell.thumbnailImageView.image = image
            cell.nameLabel.text = cellLocation.name
            cell.priceLabel.text = cellLocation.price
            cell.displayRating(location: cellLocation)
        }

        YelpClient.sharedInstance().getOpeningHoursFromID(id: cellLocation.id, completionHandlerForOpeningHours: { (isOpenNow, error) in

            if let error = error {
                print("There was an error: \(String(describing: error))")
            }

            if let isOpenNow = isOpenNow {

                performUIUpdatesOnMain {

                    if isOpenNow {
                        cell.openLabel.text = "Open"
                        cell.openLabel.textColor = UIColor.black
                    } else {
                        cell.openLabel.text = "Closed"
                        cell.openLabel.textColor = UIColor(red: 195/255, green: 89/255, blue: 75/255, alpha: 1.0)
                        cell.openLabel.font = UIFont.systemFont(ofSize: 17.0, weight: .semibold)
                    }
                }
            }
        })
    })
        }
    }
    return cell
}

numberOfRowsInSection:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return locations.count
}

让我知道我错过了什么。

1 个答案:

答案 0 :(得分:1)

创建一个只接收所需数据的类变量,即使它只是一个项目:

var ans = from N in Table_A
          join A in Table_B on N.ID equals A.ID
          from G in NotasQL
          where G.CODE == N.Not1 || G.CODE == N.Not2
          join X in Attributes on A.AppID equals X.AppID
          select new {
              N.Name,
              N.Unit,
              N.Typee,
              N.ID, // ??? not sure table for this column
              G.NOTE // ??? not sure table for this column
          };

现在使用function checkOnline(nombre, callback){ try { $.ajax({ type: 'GET', url: 'https://api.twitch.tv/kraken/streams/' + nombre, headers: { 'Client-ID': '' }, success: function(data) { // console.log(data["stream"]); if (data["stream"] == null) { callback("offline.png"); break; // console.log("offline.png"); }else { callback("online.png"); break; //console.log("online.png"); } }, error: function(data){ alert("Stream not found!"); } }); } catch (e) { alert("Conection error, try later... Monkeys are fixing it"); } return result; } 填充touch-action

numberOfRowsInSection:

touch-action: none;

cellForRowAt:

var location1Item = [location]()
locations1Item.append(locations[0]) //Or the item you want to show
相关问题