ios - 如何将水平单元格重新设计为垂直单元格

标签 ios swift uitableview

这是我所有细胞的样子。一些信息显示在单元格的右侧。而不是这种左侧有圆形半径图像而右侧有信息的设计。我希望我的单元格是一个图像,信息位于该图像的顶部。

enter image description here

这是我希望我的单元格具有的样式类型,没有图像之间的空间,我尝试通过删除信息标签并在单元格上使用水平堆栈 View 来进行此尝试。

enter image description here

但这就是细胞最终的样子。对于图像的半径,我没有以编程方式进行,而是通过 Identity Inspector 完成的

  1. 键路径 layer.cornerRadius

  2. 类型 编号

  3. 30

使图像变圆。除此之外,tableView 中没有涉及太多代码,大部分都是通过 Main.StoryBoard 完成的,下面我添加了一些代码来展示我如何配置单元格。关于如何完成此操作的任何提示或想法?

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

        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! RestaurantTableViewCell

        //* Configure the cell...
       cell.nameLabel.text = restaurants[indexPath.row].name
        cell.thumbnailImageView.image = UIImage(named: restaurants[indexPath.row].image)
        cell.locationLabel.text = restaurants[indexPath.row].location
       cell.typeLabel.text = restaurants[indexPath.row].type
        cell.accessoryType = restaurants[indexPath.row].isVisited ? .Checkmark : .None

        return cell
    }

enter image description here

最佳答案

如果您希望单元格显示图像,然后在该图像顶部显示文本,我建议您将图像设置为单元格背景 View (如果需要,将角变圆),然后填充标签和附件 View 正如您已经在做的那样。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let dictData : NSDictionary = arrayOfData.objectAtIndex(indexPath.row) as! NSDictionary

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    cell.textLabel?.text = "THIS IS SOME TEXT"

    if let image = dictData.objectForKey("Image"){
        cell.backgroundColor = UIColor.clearColor()
        let imageView = UIImageView(image: image as? UIImage)
        imageView.layer.cornerRadius = 30.0
        imageView.clipsToBounds = true
        cell.backgroundView = imageView

    }

    return cell
}

这会让你在图片上看到文字:

Custom cell background image

关于ios - 如何将水平单元格重新设计为垂直单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34527159/

相关文章:

objective-c - 检查 URL 是否为本地文件?

arrays - Swift 4 按值复制内部有数组的对象数组

ios - 保存由 textFieldDidEndEditing 更改的文本字段值不会保存在核心数据中?

ios - UITableView 的部分索引如何改变单元格的 contentView 边距?

ios - 设置Tableview的backgroundView

iOS/swift : InAppPurchase invalidProductIdentifiers

ios - Siri 快捷方式 : call API as from the containing App

ios - 在 iOS 11 中使用大标题时调整条形按钮项目的位置

ios - 重新排序 UITableView 中的部分

ios - 关闭 iOS 模式后,产生 WaitForSeconds 中断 (Unity 3D)