如何UITableViewCell在表格视图中使用s 内的自动布局,让每个单元格的内容和子视图确定行高(本身/自动),同时保持平滑的滚动性能?
如何向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) 我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)
有谁知道这个?
我正在努力弄清楚这段代码的错误.这目前在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) 我已经和Swift和iOS合作了好几个月了.我熟悉许多方法,但我不够好,我可以不用看就写出来.我很欣赏Stack Overflow过去提供的快速答案,让我回到正轨,我已经生锈了(例如,AsyncTask Android示例).
iOS UITableView对我来说属于这一类.我已经完成了几次,但我忘记了细节.我在StackOverflow上找不到另一个问题,只是要求一个基本的例子,我正在寻找比许多在线教程更短的东西(虽然这个非常好).
我将在下面提供一个答案供我将来参考和你的.
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) 我以编程方式创建的表格视图单元格未根据其自定义视图的内在内容高度进行大小调整,即使我正在使用UITableViewAutomaticDimension并设置顶部和底部约束.
问题可能在于我的UITableViewCell子类实现.请参阅下面的代码"无法以编程方式工作">"代码">"MyCustomCell.swift".
我正在尝试为自定义蒙古语键盘制作一个建议栏.蒙古文是垂直写的.在Android中它看起来像这样:
我已经知道我应该使用一个UITableView可变单元格高度,从iOS 8开始可以使用.这需要使用自动布局并告诉表格视图使用单元格高度的自动尺寸.
我一直在学习的一些事情在我最近的SO问题和答案中有所体现:
所以我已经达到了支持内在内容大小的垂直标签.这些标签放在我的自定义表格视图单元格中.并且如下一节所述,它们在我在故事板中执行时起作用,但在我以编程方式创建所有内容时则不起作用.
为了隔离问题,我创建了两个基本项目:一个用于我使用故事板的地方,另一个用于以编程方式执行所有操作.故事板项目有效.从下图中可以看出,每个表格视图单元格都会调整大小以匹配自定义垂直标签的高度.
在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) 如何使用下一个样式创建UITableViewCell(xib中的右侧详细信息)

例如,我可以使用下一个:
cell.style ='右边细节'(粗略)
谢谢!
因此,我创建了一个自定义表格视图单元格,左侧是标签,右侧是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中.我有什么想法可能做错了吗?标签都是正确的.谢谢
ios ×9
uitableview ×8
swift ×7
autolayout ×1
events ×1
ios8 ×1
nsautolayout ×1
objective-c ×1
row-height ×1
touch ×1
uiimageview ×1
uiview ×1
xcode ×1