【问题标题】:Custom UIImageView added to UItableview cell doesnot shift on on x-axis?添加到 UItableview 单元格的自定义 UIImageView 不会在 x 轴上移动?
【发布时间】:2015-03-20 03:28:41
【问题描述】:

我试图在 x 轴上为在 tableView 单元格内设置的 imageView 留出一些边距。但是 imageView 不动。我也尝试过同样的标签。它确实转移到我给出的值。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{
//  cell.textLabel?.font = UIFont(name: label.font.fontName, size: 22)
    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell
//  cell.imageView?.image =  UIImage(named: self.cellImgs[indexPath.row])
    cell.textLabel?.text = self.cellTxtArrs[indexPath.row]
    cell.textLabel?.textAlignment = NSTextAlignment.Right
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.backgroundColor = UIColor(red: 0.000, green: 0.400, blue: 0.404, alpha: 1.00)
    cell.selectionStyle =  UITableViewCellSelectionStyle.None


//  var videoImgView:UIImageView = UIImageView(frame: CGRectMake(50, 10, 20, 30.0))
//  let videoImage = UIImage(named: "accounts")
//  videoImgView = UIImageView(image: videoImage)
//  cell.contentView.addSubview(videoImgView)

    var newLabel = UILabel(frame: CGRectMake(80, 0, 80, 30.0))
    newLabel.text = "hello all"
    newLabel.textColor = UIColor.redColor()
    cell.contentView.addSubview(newLabel)

    return cell
}

我已经创建了一个表格视图

var tblView : UITableView = UITableView()
tblView.frame = CGRectMake(0, 168, 320-50 , 448)
tblView.separatorColor = UIColor.clearColor()
tblView.scrollEnabled = false
tblView.rowHeight = 39
self.addSubview(tblView)
tblView.delegate = self
tblView.dataSource = self
tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")

【问题讨论】:

  • 你的 imageview 代码在哪里?你在使用自动布局吗??
  • 注释行是图像视图代码。不,我没有使用
  • 请尝试查看我的代码

标签: ios uitableview swift uiimageview


【解决方案1】:

1) 每次调用cellForRowAtIndexPath 时,您的代码都会cell.contentView.addSubview。您没有正确地重复使用单元格。

2) 子类 UITableViewCell 并在其中添加您的“逻辑视图代码”(正确的地方)

3) 看看这个:http://www.objc.io/issue-1/lighter-view-controllers.html

【讨论】:

  • 我可以创建一个没有故事板的自定义单元格吗?
  • 当然。我讨厌故事板,我总是使用 XIB 文件。
【解决方案2】:

请尝试使用这个,希望对您有所帮助。

    videoImgView = UIImageView(image: videoImage)
    videoImgView.frame = CGRectMake(100, 10, 20, 30.0)
   cell.contentView.addSubview(videoImgView)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多