Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获取imageView';cellForRowAtIndexPath期间的帧大小?_Ios_Swift - Fatal编程技术网

Ios 如何获取imageView';cellForRowAtIndexPath期间的帧大小?

Ios 如何获取imageView';cellForRowAtIndexPath期间的帧大小?,ios,swift,Ios,Swift,我正在尝试将UITableViewController与XIB一起使用,而不是脚本,并使用默认样式:UITableViewCellStyleDefault。我想将默认的imageView的cornerRadius设置为imageView的宽度/2,但在cellforrowatinexpath()期间,imageView为0,0,0,0 我记得在某个地方读到过这样的消息,帧在添加到子视图之前不会得到更新 如何获取imageView.frame.size.width?UITableView正确显示行

我正在尝试将
UITableViewController
与XIB一起使用,而不是脚本,并使用默认样式:
UITableViewCellStyleDefault
。我想将默认的
imageView
cornerRadius
设置为
imageView
的宽度/2,但在
cellforrowatinexpath()
期间,
imageView
为0,0,0,0

我记得在某个地方读到过这样的消息,帧在添加到子视图之前不会得到更新

如何获取
imageView.frame.size.width
UITableView
正确显示行,但
cornerRadius
为0

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ContactCell", forIndexPath: indexPath)

    // Set Image
    cell.imageView?.image = UIImage.init(imageLiteral: "\(indexPath.row).jpg")

    // FOLLOWING DOESNT WORK imageView is 0,0,0,0
    cell.imageView?.layer.cornerRadius = cell.imageView!.frame.size.width / 2 

    cell.imageView?.clipsToBounds = true
    cell.imageView?.layer.masksToBounds = true

    // Set Label
    let name:String = contactsArray![indexPath.row] as! String
    cell.textLabel!.text = name

    return cell
}

实现目标的一种可能方式:
对UITableViewCell进行子类化,并覆盖
布局子视图()

override func layoutSubviews() {
    super.layoutSubviews()
    self.imageView?.layer.cornerRadius = cell.imageView!.frame.size.width / 2
}

实现目标的一种可能方式:
对UITableViewCell进行子类化,并覆盖
布局子视图()

override func layoutSubviews() {
    super.layoutSubviews()
    self.imageView?.layer.cornerRadius = cell.imageView!.frame.size.width / 2
}

下面的行还不会布局任何UI,所以此时您无法获得任何类型的尺寸

cell.imageView?.image = UIImage.init(imageLiteral: "\(indexPath.row).jpg")
一种方法是获取图像大小

let image = UIImage.init(imageLiteral: "\(indexPath.row).jpg")
cell.imageView?.image = image
let size = image.size
然后用这个尺寸做剩下的


但是如果你的图像太大,那么你可能需要寻找相应的方法来调整它的大小,因为它会根据实际图像的大小来加载图像,并且它可能不像你期望的那样

下面的行还不会布局任何UI,所以此时您无法获得任何类型的尺寸

cell.imageView?.image = UIImage.init(imageLiteral: "\(indexPath.row).jpg")
一种方法是获取图像大小

let image = UIImage.init(imageLiteral: "\(indexPath.row).jpg")
cell.imageView?.image = image
let size = image.size
然后用这个尺寸做剩下的


但是如果你的图像太大,那么你可能需要寻找相应的方法来调整它的大小,因为它会根据实际图像的大小来加载图像,并且它可能不像你期望的那样

只是想知道,您是否给imageView提供了占位符图像或高度/宽度限制?没有。因为这是在XIB中,我不能使用原型单元格,我也没有使用自定义单元格XIB。我所做的只是在viewDidLoad:tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:“ContactCell”)中注册默认的UITableViewCell。在XIB中没有做任何事情,主要是因为我不能,因为它是XIB而不是故事板。我可能只需要创建自己的UITableViewCell并为其提供一些默认值。我从未使用过
UIImage.init(imageLiteral:\(indexPath.row.jpg”)
,我总是使用
UIImage(name:\(indexPath.row.jpg)
,它运行吗?更改为UIImage.init(名称:…相同。尝试使用图像的大小。image.size.width/2只是想知道,您是否为imageView提供了占位符图像或高度/宽度约束?否。因为这是在XIB中,所以我无法使用原型单元格,我也没有使用自定义单元格XIB。我所做的只是在viewDidLoad:tableView中注册默认的UITableViewCell。registerClass(UITableViewCell.self,forCellReuseIdentifier:“ContactCell”)。在XIB中没有做任何事情,主要是因为它是XIB而不是故事板。我可能只需要创建自己的UITableViewCell并为其提供一些默认值。我从未使用过
UIImage.init(imageLiteral:\(indexath.row.jpg))
,我总是使用
UIImage(名称:“\(indexath.row.jpg”)
,是否运行?更改为UIImage.init(名称:…相同。尝试使用图像的大小。image.size.width/2是的,这就是它的外观,单元格还没有尺寸。唯一有尺寸的是我的图像,是的,它不是我想要的大小。我将尝试创建一个自定义的XIB单元格,并为其提供一些默认值。我想我可以使用苹果的默认值以最小的方式实现这一点UITableViewCell,但看起来可能不是。是的,这就是它的外观,单元格还没有尺寸。唯一有尺寸的是我的图像,是的,它不是我想要的大小。我将尝试创建一个自定义XIB单元格,并为它提供一些默认值。我想我可以使用苹果默认的UITableViewCell,但看起来很简单这可能不是。谢谢,这成功了!我创建了一个自定义的UITableViewCell.xib和.swift文件。虽然这看起来很简单,但我希望使用默认的UITableViewCell类,该类中已经有一个imageView。谢谢,这成功了!我创建了一个自定义的UITableViewCell.xib和.swift文件。虽然这看起来很简单,但我希望使用已包含imageView的默认UITableViewCell类。