无法设置tableView单元格插入

时间:2015-03-05 23:20:15

标签: xcode swift ios8

好的,我正在以编程方式创建一个tableview及其各自的单元格。我有两个问题我无法解决。

我不知道如何设置插图以便在单元格边缘和文本之间创建足够的空间。我是新手,因此我可能不正确,但是我有可以使用的功能吗?

以下是我用于生成tableview及其单元格的代码:

//
//  HashtagController.swift
//  InstagramApplication
//
//  Created by jshbrmn on 2015-03-05.
//  Copyright (c) 2015 2gen. All rights reserved.
//


import UIKit

class HashtagController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

  var model:ModelData!
  var cellCount = 0
  var currentCell: UITableViewCell!

  var tableView: UITableView = UITableView()

  override func viewDidLoad() {
    self.automaticallyAdjustsScrollViewInsets = true
    super.viewDidLoad()

    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CherrySwash-Regular", size: 25)!,  NSForegroundColorAttributeName: UIColor(red:27.0/255, green: 145.0/255, blue: 114.0/255, alpha: 1.0)]
  }

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

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cat = Array(Array(model.quoteItems.values)[cellCount])[0] as String

    if(cat == model.categoryName) {
      println(model.categoryName)
      println(model.quoteSelection())
    }

    let cellIdentifier = "Cell"

    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell

    if cell == nil {
      cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
    }

    cell?.textLabel?.text = Array(Array(model.quoteItems.values)[cellCount])[0] as? String
    cell?.textLabel?.backgroundColor = UIColor(red:26, green: 34, blue: 48, alpha: 0.0)
    cell?.textLabel?.textColor = UIColor.whiteColor()
    cell?.textLabel?.highlightedTextColor = UIColor(red:26.0/255.0, green: 34.0/255.0, blue: 48.0/255.0, alpha: 1.0)
    cell?.textLabel?.font = UIFont(name:"Helvetica-Light", size: 14.0)
    cell?.textLabel?.numberOfLines = 0
    cell?.textLabel?.lineBreakMode = .ByWordWrapping

    cell?.detailTextLabel?.text = Array(model.quoteItems.keys)[cellCount]
    cell?.detailTextLabel?.backgroundColor = UIColor(red:26, green: 34, blue: 48, alpha: 0.0)
    cell?.detailTextLabel?.textColor = UIColor(red:32.0/255.0, green: 178.0/255.0, blue: 139.0/255.0, alpha: 1.0)
    cell?.detailTextLabel?.font = UIFont(name:"Helvetica-Light", size: 14.0)

    cell?.backgroundColor = UIColor(red:26, green: 34, blue: 48, alpha: 0.0)

    var bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.whiteColor()
    cell?.selectedBackgroundView = bgColorView
    cell?.separatorInset = UIEdgeInsetsZero
    cell?.layoutIfNeeded()
    cellCount++
    return cell!
  }

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
    var currentLabel = currentCell.textLabel?.text
    var currentAuthor = currentCell.detailTextLabel?.text

    model.quote = currentLabel!
    model.author = currentAuthor!
  }

  override func viewWillAppear(animated: Bool) {
    model = (self.tabBarController as CaptionTabBarController).model

    self.automaticallyAdjustsScrollViewInsets = true
    super.viewDidLoad()
    tableView.frame         =   CGRectMake(0, 10, view.frame.width, view.frame.height);
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.backgroundColor = UIColor(red:26, green: 34, blue: 48, alpha: 0.0)
    //tableView.separatorStyle = UITableViewCellSeparatorStyle(rawValue: 0)!
    tableView.separatorColor = UIColor.blackColor()
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    self.view.addSubview(tableView)
  }
}

这是目前的列表:

enter image description here

对于每个单元格,顶部,底部,左侧和右侧需要更多填充。

另外,作为旁白。是否有适当的方法(即不那么hacky)去除整个桌子上的左边插图?

2 个答案:

答案 0 :(得分:0)

您必须将名为preservesSuperviewLayoutMargins的单元格属性设置为false

cell.preservesSuperviewLayoutMargins = false

他们只需要将单元格layoutMargins设置为零

cell.layoutMargins = UIEdgeInsetsZero

答案 1 :(得分:-1)

你可以试试这个:

self.tableView.contentInset = UIEdgeInsetsMake(30, 15, 50, 0);

但我认为这可以帮到你很多,请查看Dynamic Table View Cell Height and Auto Layout