在 UIViewController 中使用 tableView 的标签来更新标签
Use tags for tableView inside UIViewController to update Labels
我向我的 UIViewController 添加了一个 table 视图。然后我在 table 视图内的自定义单元格中添加了一个标签。我尝试使用标签更新 table 视图中的标签。似乎它不工作。
这是我的代码。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
当我调用该函数时,它始终不读取标签并打印 "fail"。我也为我的标签分配了正确的标签值。
这里我在属性检查器中附上了我的标签详细信息的图像
请帮我解决这个问题。
正如@TheAppMentor 所说,代码看起来不错。确保您输入了正确的单元格标识符。然后确保连接了视图控制器以更正 class(这是我的常见错误)。还要确保此单元格中的其他内容没有相同的标签。您还可以打印所有子视图以检查其中的内容。
你的手机不知道里面有你的标签(那个有 500 标签的)。由于这一行,您没有提供自定义单元格:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
这是预定义的单元格。根据文档,您可以拥有预定义的单元格或自定义单元格(通过 subclassing UITableViewCell 获得):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
如果要预定义:
如果你只想将文本放在单元格上使用(我看到这个评论放在第一位)。
cell.textLabel?.text = object["quantity"]
去定制
在单独的 swift 文件上扩展 UITableViewCell。在这里进行绑定并并行处理故事板。将自定义 class 分配给您的单元格。另外,更改出队:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell
我向我的 UIViewController 添加了一个 table 视图。然后我在 table 视图内的自定义单元格中添加了一个标签。我尝试使用标签更新 table 视图中的标签。似乎它不工作。
这是我的代码。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
当我调用该函数时,它始终不读取标签并打印 "fail"。我也为我的标签分配了正确的标签值。
这里我在属性检查器中附上了我的标签详细信息的图像
请帮我解决这个问题。
正如@TheAppMentor 所说,代码看起来不错。确保您输入了正确的单元格标识符。然后确保连接了视图控制器以更正 class(这是我的常见错误)。还要确保此单元格中的其他内容没有相同的标签。您还可以打印所有子视图以检查其中的内容。
你的手机不知道里面有你的标签(那个有 500 标签的)。由于这一行,您没有提供自定义单元格:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
这是预定义的单元格。根据文档,您可以拥有预定义的单元格或自定义单元格(通过 subclassing UITableViewCell 获得):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
如果要预定义: 如果你只想将文本放在单元格上使用(我看到这个评论放在第一位)。
cell.textLabel?.text = object["quantity"]
去定制
在单独的 swift 文件上扩展 UITableViewCell。在这里进行绑定并并行处理故事板。将自定义 class 分配给您的单元格。另外,更改出队:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell