2017-02-07 7 views
0

私はキャラクタにネストされたプログラマチックテーブルビューを持っていますが、それが表示されると、プログラムセルは常にテーブルビューよりも広く設定されます。カルーセル項目の幅、 281,281,320(iphone5画面の幅)。私はセルに制約を追加しようとしましたが、無限のエラーが発生し続けると、セルはまだ作成されていないと信じています。だから私はこれらのエラーを停止するように制約を配置する場所がわからない、私はテーブルビュー幅に一致するセルの内容のビューの幅が必要ですが、どのようにプログラムで作られている私のカスタムセルでテーブルビューの幅を取得するのですか? ?相続人は制約のない私の現在のコード:セル幅がテーブルビューの幅には大きすぎます

のtableView:

import UIKit 

class SplitterCarouselItemTableView: UITableView { 

    var splitter: BillSplitter? 

    required init(frame: CGRect, style: UITableViewStyle, splitter: BillSplitter) { 
     super.init(frame: frame, style: style) 
     self.splitter = splitter 
     setupView() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setupView() { 

     if Platform.isPhone { 
      let tableViewBackground = UIImageView(image: UIImage(data: splitter?.image as! Data, scale:1.0)) 
      self.backgroundView = tableViewBackground 
      tableViewBackground.contentMode = .scaleAspectFit 
      tableViewBackground.frame = self.frame 
     } 

     self.backgroundColor = .clear 
     self.separatorStyle = .none 
    } 
} 

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

    tableView.register(SplitterCarouselItemTableViewCell.classForCoder(), forCellReuseIdentifier: "splitterCarouselItemTableViewCell") 

    let cell: SplitterCarouselItemTableViewCell = tableView.dequeueReusableCell(withIdentifier: "splitterCarouselItemTableViewCell") as! SplitterCarouselItemTableViewCell 
    var item = ((allBillSplitters[tableView.tag].items)?.allObjects as! [Item])[indexPath.row] 
    if allBillSplitters[tableView.tag].isMainBillSplitter { 
     getMainBillSplitterItems(splitter: allBillSplitters[tableView.tag]) 
     item = mainBillSplitterItems[indexPath.row] 
    } 
    let count = item.billSplitters?.count 

    if count! > 1 { 
     cell.name!.text = "\(item.name!)\nsplit \(count!) ways" 
     cell.price!.text = "£\(Double(item.price)/Double(count!))" 

    } else { 
     cell.name!.text = item.name! 
     cell.price!.text = "£\(item.price)" 
    } 

    return cell 
} 

セル:

import UIKit 

class SplitterCarouselItemTableViewCell: UITableViewCell { 

    var name: UILabel! 
    var price: UILabel! 
    var view: UIView! 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:)") 
    } 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: "splitterCarouselItemTableViewCell") 

     self.backgroundColor = .clear 

     let width = Int(contentView.bounds.width) 
     let height = Int(contentView.bounds.height) 

     view = UIView(frame: CGRect(x: 0, y: 2, width: width, height: height - 4)) 
     view.backgroundColor = UIColor.white.withAlphaComponent(0.5) 

     let viewHeight = Int(view.bounds.height) 
     let viewWidth = Int(view.bounds.width) 

     price = UILabel(frame: CGRect(x: viewWidth - 80, y: 0, width: 75, height: viewHeight)) 
     price.font = UIFont.systemFont(ofSize: 15.0) 
     price.textAlignment = .right 

     name = UILabel(frame: CGRect(x: 5, y: 0, width: width, height: viewHeight)) 
     name.font = UIFont.systemFont(ofSize: 15.0) 
     name.numberOfLines = 0 

     view.addSubview(name) 
     view.addSubview(price) 

     contentView.addSubview(view) 
    } 
} 

私はinitが全てそのコードで満たされるべきではないと、後に抽出されます理解しています。私は幅の問題を解決するまではちょうどそこに。

ありがとうございました。

+0

'UIScreen.mainScreen()。bounds.width'を使用して、画面の幅を正確に取得します。 – iphonic

+0

あなたは 'cellForRowAt:indexPath'にあなたのセルを登録すべきではありません。おそらく問題を引き起こしています。 'viewDidLoad'のtableViewにセルを登録します。 –

+0

@ WolfgangSchreursありがとう私はそれを移動しましたが、何も変わっていません。 – Wazza

答えて

1

UITableViewCellクラスでは、tableView.dequeueReusableCell(withIdentifier:)メソッドがinit(frame:)を間接的に呼び出すという課題があります。 SplitterCarouselItemTableViewCellに上書きinit(frame:)

  1. 、あなたがsuper.init(frame:)
  2. 呼び出すときに自動レイアウトが持つ問題がある使用して、あなたのfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)方法

であなたのcell.frameを変更しますが、あなたの幅を設定します。そこにこれを解決する方法はいくつかありますUITableViewセル。あなたはそこにあるすべてのサブビューを使いこなすつもりはありません。

+0

ありがとう、cellForRowAtで動作していなかったので、私はセルのinitでフレームを設定する必要がありました – Wazza

関連する問題