2016-04-27 185 views
1

我在故事板中创建了一个自定义的UITableView单元格。以前,我对单元格使用了“Subtitle”风格,但是我现在使用由2个UILabels组成的定制设计。UILabels和自定义的UITableView单元格

我现在怎样才能访问这些UILabel?之前我用这两个自定义UILabels我只想用:

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


cell.textLabel?.text = pollContent 
cell.detailTextLabel?.text = dateString 

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

方法

但由于单元格样式现在设置为“自定义”我真的不能

这样做了。 我遇到的问题是,我真的不知道如何使用IBOutlets以某种方式将这两个UILabel连接到ViewController类?

我还创建了一个自定义的UITableViewCell类,并将UILabels连接到该类,但是如何从ViewController获取每个单元的UILabel内容?

UPDATE: 所以我投的细胞与我的自定义的UITableViewCell现在,我添加下面的代码行上我的课:

self.textLabel = titleLabel 
    self.detailTextLabel = subTitleLabel 

其中titleLabel和subTitleLabel都是我UILabels的。

但是,我收到错误消息.textLabel和.detailTextLabel都是只读属性。

更新2: 感谢您的所有答案/建议。正如你可能猜到的,我仍然是编程的新手!

当我解决我的问题时,我会回来。

更新3:再次感谢您的帮助!它现在终于起作用了。

+0

1.您需要创建一个UITableViewCell类。将您的IBOutlets添加到此自定义类。设置Cell claa – Cloy

+0

[在swift中创建自定义tableview单元格]的可能的重复(http://stackoverflow.com/questions/24170922/creating-custom-tableview-cells-in-swift) –

+0

这些都不能解决我的问题@AlessandroOrnano – user3545063

回答

2

您需要将单元格转换为您的自定义单元格。

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


cell.customLabel1.text = pollContent 
cell.customLabel2.text = dateString 
+0

感谢您的快速响应!我必须缺少一些东西,UILabels仍然只显示默认文本。 单元格如何知道textLabel和detailTextLabels引用了我的UILabels? – user3545063

+0

更新了我的问题 – user3545063

+0

确保Storyboard中的单元格设置为自定义单元格。然后,您创建自定义单元格文件的插座。 – rMickeyD

0

1.您需要创建一个可扩展UITableViewCell的CustomUITableViewCell类。

2.将您的IBOutlets添加到此CustomUITableViewCell中。

3.将Cell类设置为您刚刚在故事板中创建的CustomUITableViewCell。

4.change这

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

    cell.textLabel?.text = pollContent 
    cell.detailTextLabel?.text = dateString 
0

请尝试:

self.textLabel.text = titleLabel 
self.detailTextLabel.text = subTitleLabel 

有关设置文本insinde这些标签。

0

看来你必须设置小区标识也在属性检查器

enter image description here

0

设置一些独特的价值,以你的uilabels标记属性。后来

让他们在

cellForItemAtIndexPath

像这样:

UILabel *label = (UILabel*)[cell viewWithTag:yourTagNumber]; 

BTW:不要试图指派为textLabel和detailTextLabel当你的电池的风格是Custom 。只有在款式为Subtitle时才可使用。否则,他们是零。

相关问题