【发布时间】:2018-08-20 02:20:40
【问题描述】:
我正在尝试下载图像并将其放置在包含 UIImage 和 UITextView 的自定义单元格中。我需要它等于屏幕的宽度并具有成比例的高度,类似于 question 但我不知道如何使用它来转换答案以使用自定义表格单元格。在尝试在tableView:cellForRowAt 中设置图像宽度和高度时,调用cell.storedImage.frame.width 和height Xcode 说它只是一个get-only 属性。这是我认为可行的方法
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableData[indexPath.row]["Image"] != nil {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageNotesData", for: indexPath) as! ImageNotesCell
cell.notes.delegate = self
cell.notes.tag = indexPath.row
cell.notes.text = tableData[indexPath.row]["Notes"] as! String
guard let imageFirebasePath = tableData[indexPath.row]["Image"] else {
return cell }
let pathReference = Storage.storage().reference(withPath: imageFirebasePath as! String)
pathReference.getData(maxSize: 1 * 1614 * 1614) { data, error in
if let error = error {
print(error)
} else {
//let image = UIImage(data: data!)
//cell.storedImage.image = image
let containerView = UIView(frame: CGRect(x:0,y:0,width:self.view.frame.width
,height:500))
let imageView = UIImageView()
if let image = UIImage(data:data!) {
let ratio = image.size.width / image.size.height
if containerView.frame.width > containerView.frame.height {
let newHeight = containerView.frame.width / ratio
imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
cell.storedImage.frame.height = newHeight
}
else{
let newWidth = containerView.frame.height * ratio
imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
cell.storedImage.frame.width = newWidth
}
}
let image = UIImage(data: data!)
cell.storedImage.image = image
}
}
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "notesData", for: indexPath) as! NotesCell
//let noteString = tableData[indexPath.row]["Notes"] as! String
cell.notes.text = tableData[indexPath.row]["Notes"] as! String
cell.notes.delegate = self
cell.notes.tag = indexPath.row
return cell
}
}
我还尝试使用自动布局来解决问题,方法是让其下方的文本视图具有两个不同的下边距约束,但它仍然看起来不正确。这是我得到的带有自动布局约束的壁橱。目前,图像视图在超级视图的每一侧都有 0 个空间限制
我假设代码 sn-p 是我应该使用的路径,但我不确定如何使图像看起来正确。
【问题讨论】:
-
你解决了吗?
-
不,我没有,我只是放弃了它。
-
尽快告诉我这个问题。你需要图像和标签之间的间隙吗?
-
@LampPost 您是否通过我的回答或其他原因解决了您的问题?如果您自己找到答案,请随时分享并将其作为答案,您永远不知道谁会来寻找它,或者我的答案是否有助于您随意将其标记为正确。
标签: ios swift uitableview uiimageview uiimage