相关疑难解决方法(0)

在UITableView中使用自动布局来获取动态单元格布局和可变行高

如何UITableViewCell在表格视图中使用s 内的自动布局,让每个单元格的内容和子视图确定行高(本身/自动),同时保持平滑的滚动性能?

row-height uitableview ios autolayout nsautolayout

1477
推荐指数
18
解决办法
55万
查看次数

如何向UIView添加触摸事件?

如何向UIView添加触摸事件?
我尝试:

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, nextY)] autorelease];
[headerView addTarget:self action:@selector(myEvent:) forControlEvents:UIControlEventTouchDown];
// ERROR MESSAGE: UIView may not respond to '-addTarget:action:forControlEvents:'
Run Code Online (Sandbox Code Playgroud)

我不想创建子类和覆盖

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Run Code Online (Sandbox Code Playgroud)

events objective-c touch uiview ios

272
推荐指数
9
解决办法
32万
查看次数

致命错误:在解包可选值时意外发现nil

UICollectionView在Swift 中使用了一个,但是当我尝试更改单元格标签的文本时,我得到了.

    func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
    return 5
}

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("title", forIndexPath: indexPath) as TitleCollectionViewCell
    // Next line: fatal error: unexpectedly found nil while unwrapping an Optional value
    cell.labelTitle.text = "This is a title"
    return cell
}
Run Code Online (Sandbox Code Playgroud)

有谁知道这个?

控制台上警告的屏幕截图

ios uicollectionview swift

125
推荐指数
6
解决办法
25万
查看次数

Swift中的UITableView

我正在努力弄清楚这段代码的错误.这目前在Objective-C中工作,但在Swift中,这只是在方法的第一行崩溃.它在控制台日志中显示错误消息:Bad_Instruction.

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
        }

        cell.textLabel.text = "TEXT"
        cell.detailTextLabel.text = "DETAIL TEXT"

        return cell
    }
Run Code Online (Sandbox Code Playgroud)

uitableview swift ios8

60
推荐指数
6
解决办法
10万
查看次数

Swift的UITableView示例

我已经和Swift和iOS合作了好几个月了.我熟悉许多方法,但我不够好,我可以不用看就写出来.我很欣赏Stack Overflow过去提供的快速答案,让我回到正轨,我已经生锈了(例如,AsyncTask Android示例).

iOS UITableView对我来说属于这一类.我已经完成了几次,但我忘记了细节.我在StackOverflow上找不到另一个问题,只是要求一个基本的例子,我正在寻找比许多在线教程更短的东西(虽然这个非常好).

我将在下面提供一个答案供我将来参考和你的.

uitableview ios swift

59
推荐指数
3
解决办法
14万
查看次数

'required'initulator'init(coder :)'必须由'UITableViewCell'的子类提供

据报道,此代码在这里这里都有用,但我似乎无法使其工作.

IBOutlets连接到故事板中的对象.prototypeCell被命名为我可以使用它dequeueReusableCellWithIdentifier并且它的自定义类属性被设置为commentCell.

第一个错误(我可以解决,但上面的链接都不需要它,这让我觉得我做错了.我是对的吗?):

Overriding method with selector 'initWithStyle:reuseIdentifier:' has incompatible type '(UITableViewCellStyle, String) -> commentCell'
Run Code Online (Sandbox Code Playgroud)

第二个错误(有趣的错误):

'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`
Run Code Online (Sandbox Code Playgroud)

细胞类代码:

class commentCell: UITableViewCell {
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var commentLabel: UITextView!

    init(style: UITableViewCellStyle, reuseIdentifier: String) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}
Run Code Online (Sandbox Code Playgroud)

初始化代码:

func tableView(tableView: …
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift

26
推荐指数
1
解决办法
3万
查看次数

具有可变单元格高度的UITableView:在IB中工作但不是以编程方式工作

TL; DR

我以编程方式创建的表格视图单元格未根据其自定义视图的内在内容高度进行大小调整,即使我正在使用UITableViewAutomaticDimension并设置顶部和底部约束.

问题可能在于我的UITableViewCell子类实现.请参阅下面的代码"无法以编程方式工作">"代码">"MyCustomCell.swift".

目标

我正在尝试为自定义蒙古语键盘制作一个建议栏.蒙古文是垂直写的.在Android中它看起来像这样:

在此输入图像描述

进展

我已经知道我应该使用一个UITableView可变单元格高度,从iOS 8开始可以使用.这需要使用自动布局并告诉表格视图使用单元格高度的自动尺寸.

我一直在学习的一些事情在我最近的SO问题和答案中有所体现:

所以我已经达到了支持内在内容大小的垂直标签.这些标签放在我的自定义表格视图单元格中.并且如下一节所述,它们在我在故事板中执行时起作用,但在我以编程方式创建所有内容时则不起作用.

在IB工作

为了隔离问题,我创建了两个基本项目:一个用于我使用故事板的地方,另一个用于以编程方式执行所有操作.故事板项目有效.从下图中可以看出,每个表格视图单元格都会调整大小以匹配自定义垂直标签的高度.

在此输入图像描述

在IB

我设置约束来固定顶部和底部以及使标签居中.

在此输入图像描述

ViewController.swift

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let myStrings: [String] = ["a", "bbbbbbb", "cccc", "dddddddddd", "ee"]
    let cellReuseIdentifier = "cell"

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    // number of rows in …
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift mongolian-vertical-script

25
推荐指数
2
解决办法
1311
查看次数

UITableViewCell以编程方式设置样式

如何使用下一个样式创建UITableViewCell(xib中的右侧详细信息)

在此输入图像描述

例如,我可以使用下一个:

cell.style ='右边细节'(粗略)

谢谢!

uitableview ios

8
推荐指数
1
解决办法
2万
查看次数

自定义表格视图单元格,带有swift in xcode 6

因此,我创建了一个自定义表格视图单元格,左侧是标签,右侧是UIImageView.标签的标签为100,UIImageView的标签为110.

我的代码如下:

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

    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("ThemeCell") as UITableViewCell

    let theme = themes[indexPath.row]

    var themeName = cell.viewWithTag(100) as? UILabel
    themeName?.text = theme.themeName

    let themeImage = cell.viewWithTag(110) as? UIImageView
    themeImage?.image = UIImage(named: "test.png")

    //1
    //cell.imageView?.image = UIImage(named: "test.png")

    println("The loaded image: \(themeImage?.image)")

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

按原样,显示themeName但不显示themeImage,尽管从println看起来似乎已加载图像.如果我取消注释1中的代码,图像将出现在自定义单元格中,但当然不会出现在正确的位置,因为它没有添加到我在IB中创建的UIImageView中.我有什么想法可能做错了吗?标签都是正确的.谢谢

xcode uitableview uiimageview ios swift

8
推荐指数
1
解决办法
1万
查看次数

如何在TableView中更改Xcode中原型单元格的高度?

我已经拖入了UITableView我的故事板,我可以完全插入信息.但为了定制单元格,我添加了原型单元格.当我手动更改它的高度时,模拟器中没有任何变化.

在此输入图像描述

uitableview ios swift

6
推荐指数
2
解决办法
6111
查看次数