简体   繁体   English

Swift XCode-情节提要中的更改未反映在iPhone模拟器中

[英]Swift XCode - Changes in storyboard aren't being reflected in the iPhone simulator

I'm trying to make a simple UITableView that has a button appended to the right side of every cell. 我正在尝试制作一个简单的UITableView,该UITableView的每个单元格的右侧都有一个按钮。 I added the button to the prototype cell in my storyboard and set up the constraints so that AutoLayout takes over and puts it in the right spot. 我将按钮添加到情节提要中的原型单元并设置了约束,以便AutoLayout接管并将其放置在正确的位置。 When I go to preview the layout in the Assistant Editor, everything looks great. 当我在助手编辑器中预览布局时,一切看起来都很棒。 However, once I start the simulator and go to that view, the button is no longer there (the cell is blank). 但是,一旦启动模拟器并转到该视图,该按钮将不再在那里(单元格为空白)。

I ran into a similar problem when I tried to add a Disclosure Indicator and it wasn't showing up - to fix it I had to add that accessory programatically. 当我尝试添加一个披露指示器并且它没有显示时,我遇到了一个类似的问题-要解决该问题,我必须以编程方式添加该附件。 Isn't the point of storyboard to help make changes without using code? 故事板无需使用代码即可帮助进行更改的目的不是吗? I'm still really new at this and would appreciate some guidance. 我仍然很新,不胜感激。

EDIT: Some code as requested (sorry): 编辑:一些要求的代码(对不起):

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

    let addButton = UIButton()

    let menuItem = currentOrder?.getSortedMenuItems()[indexPath.row]
    let itemPrice = "\(currentOrder!.menu[menuItem!]!)"

    cell.textLabel!.text = menuItem! + " - " + itemPrice
    return cell
}

Most of the code in there is pretty irrelevant, but as you can see I tried adding a button and then stopped cause I wasn't sure where to go from there. 那里的大多数代码都无关紧要,但是如您所见,我尝试添加一个按钮然后停止了,因为我不确定从那里去哪里。 I was under the impression that all of this could be done using storyboard. 我的印象是所有这些都可以使用情节提要来完成。

The problem which you have arises from the fact that you are NOT using the cells which you created in your storyboard. 您遇到的问题是由于您没有使用在情节提要中创建的单元而引起的。 When you call 你打电话时

let cell = UITableViewCell()

it creates a cell with default style. 它创建具有默认样式的单元格。 What you want to do instead is to use the cells which you created in the storyboard. 您要做的是使用在情节提要中创建的单元格。

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

// setup your cells (e.g. update its title)

return cell

} }

You should change the "cellWithSwitch" to the identifier which you have set for your cell in storyboard. 您应该将“ cellWithSwitch”更改为您在情节提要中为单元格设置的标识符。

You can find the identifier of your cell by selecting it in the storyboard and then checking info in the Attributes inspector: 您可以通过在情节提要中选择单元格的标识符,然后在“属性”检查器中检查信息来找到单元格的标识符:

在此处输入图片说明

If you haven't yet set this identifier - set it and then use it. 如果尚未设置此标识符,请先设置它,然后再使用它。 And then changes which you make in storyboard will get reflected on the phone ;) 然后,您在情节提要中所做的更改将反映在手机上;)

Extra 额外

It is often necessary to subclass your cell to make it easier to work with. 通常有必要对您的单元进行子类化以使其易于使用。 Here is an example: 这是一个例子:

class MyCell:UITableViewCell {

    @IBOutlet var myButton: UIButton!

}

Now, you have to do 3 other things: 现在,您还必须做三件事:

1) Select your cell in the storyboard and set its class to MyCell in Identity inspector (that's the third tab in the image which I posted) 1)在情节MyCell中选择您的单元格, MyCell在Identity inspector中将其类设置为MyCell (这是我发布的图像中的第三个选项卡)

2) Open Connections inspector (last tab) and connect the myButton outlet 2)打开“连接”检查器(最后一个选项卡),然后连接myButton插座

3) Modify your cellForRowAtIndexPathFunction : 3)修改您的cellForRowAtIndexPathFunction

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellWithSwitch") as! MyCell

// setup your cells (e.g. update its title)

// you can now access your button as cell.myButton

return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM