SORU
3 HAZİRAN 2014, Salı


UİTableView Swift

Bu birkaç saat kadar kaldım, Bu sorunun ne olduğunu çözemedim. Bu yöntemi ilk satırında Amaç C. Bu sadece çöker 2 saniye sonra çalışmaya devam eder. Bir hata iletisi sadece eğlenceli vermez 'Bad_İnstruction' şey

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Component") as UITableViewCell

        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Component")
        }

        cell.textLabel.text = "Baking Soda"
        cell.detailTextLabel.text = "1/2 cup"

        return cell
    }

CEVAP
4 HAZİRAN 2014, ÇARŞAMBA


Hadi özel alt veya uç oluşturmadan bir çözüm bulmak

Asıl sorun, Swıft boş (nil) nesne ve boş olamaz nesneler arasında bir ayrım yaptığı gerçeği. Eğer tanımlayıcı bir ucu kayıt yok eğer doğru değilse, o zaman dequeueReusableCellWithIdentifier nil dönebilirsiniz.

İsteğe bağlı olarak değişken bildirmek zorunda olduğumuz anlamına gelir:

var cell : UITableViewCell?

ve kullanma as? as döküm değil

//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}

//we know that cell is not empty now so we use ! to force unwrapping

cell!.textLabel.text = "Baking Soda"
cell!.detailTextLabel.text = "1/2 cup"

cell!.textLabel.text = "Hello World"

return cell

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ben Vivona

    Ben Vivona

    24 Ocak 2010
  • footiecyclo

    footiecyclo

    26 EYLÜL 2009
  • Sergio Fernandez

    Sergio Ferna

    1 EKİM 2009