简体   繁体   English

“条件渲染”如何在 Swift/Xcode 中工作?

[英]How does 'conditional rendering' work in Swift / Xcode?

In React and other web front-end frameworks, there is the concept of 'conditional rendering'.在 React 和其他 Web 前端框架中,有“条件渲染”的概念。 You render certain elements of the UI to the DOM based on certain conditions being true or not.您可以根据某些条件是否为真将 UI 的某些元素呈现给 DOM。

How does the concept of conditional rendering work in Xcode / Swift?条件渲染的概念在 Xcode / Swift 中是如何工作的? Since you apparently build the UI on the storyboard, I don't know how to conditionally render elements.由于您显然在情节提要上构建了 UI,因此我不知道如何有条件地呈现元素。

A specific example: Let's say I've got an array of arrays and for each array in the array I want to render a tableview.一个具体的例子:假设我有一个数组数组,对于数组中的每个数组,我想呈现一个 tableview。 How would I approach this problem?我将如何解决这个问题?

Short answer: "It doesn't."简短的回答:“没有。” That's not how iOS works.这不是iOS的工作方式。


Edit:编辑:

You can certainly write code that builds your UI from code, and if you do that, you're free to use conditional logic to decide what elements to add to your UI.您当然可以编写从代码构建 UI 的代码,如果这样做,您就可以自由地使用条件逻辑来决定将哪些元素添加到您的 UI 中。

In fact, SwiftUI uses code to build a UI, and is a reactive style development platform.实际上,SwiftUI 使用代码来构建 UI,是一个响应式风格的开发平台。 You should look into that.你应该调查一下。

A UITableView, as in your example, is a data-driven UI element.在您的示例中,一个 UITableView 是一个数据驱动的 UI 元素。 It uses a data source to decide what cells to display.它使用数据源来决定要显示哪些单元格。 Depending on the types of entries in the data source, it might create different types of cells.根据数据源中条目的类型,它可能会创建不同类型的单元格。 That is similar to what you describe.这和你描述的很相似。

It doesn't really make sense to have multiple table views on screen at once, so I'm not sure what "...for each array in the array I want to render a tableview" would look like.一次在屏幕上显示多个表格视图并没有什么意义,所以我不确定“......对于我想呈现表格视图的数组中的每个数组”会是什么样子。

I guess you could write a view controller that had a scroll view as it's content view, and when it loaded, it took your array of arrays, and used the outer-most array to decide how many instances of table views (or table view controllers, better yet) to instantiate.我猜你可以写一个视图控制器,它有一个滚动视图作为它的内容视图,当它加载时,它使用你的数组数组,并使用最外面的数组来决定有多少个表视图实例(或表视图控制器,更好)来实例化。 It would create table views/table view controllers in a loop, and install their views into the scroll view, keeping track of the geometry.它将在循环中创建表视图/表视图控制器,并将它们的视图安装到滚动视图中,跟踪几何形状。 Altnernately, you could put the table views in a stack view, and put the stack view into a scroll view.或者,您可以将表视图放在堆栈视图中,并将堆栈视图放入滚动视图。

However, the UX would be awkward.但是,用户体验会很尴尬。 The user would need to drag up and down inside each table view to navigate inside it, and also drag up and down on the scroll view in order to move between table views.用户需要在每个表视图内上下拖动以在其中导航,还需要在滚动视图上上下拖动以在表视图之间移动。

Let's say I've got an array of arrays and for each array in the array I want to render a tableview.假设我有一个数组数组,对于数组中的每个数组,我想呈现一个 tableview。 How would I approach this problem?我将如何解决这个问题?

You'd probably display a list of all the arrays (or whatever each array corresponds to) and a table.您可能会显示所有数组(或每个数组对应的任何内容)和表格的列表。 When the user selected one of the things in the list, you could then set the table view's data source to use the corresponding array.当用户选择列表中的一项时,您可以设置表视图的数据源以使用相应的数组。

That's not the only way, of course.当然,这不是唯一的方法。 If you really want to draw different tables for each array in the list, you can do that — you're in charge of your code.如果你真的想为列表中的每个数组绘制不同的表格,你可以这样做——你负责你的代码。 But why make things more complicated?但是为什么要把事情弄得更复杂呢?

Since you apparently build the UI on the storyboard, I don't know how to conditionally render elements.由于您显然在情节提要上构建了 UI,因此我不知道如何有条件地呈现元素。

Storyboards aren't the only way to build a UI, and they're also a lot more flexible than just a static collection of elements.故事板并不是构建 UI 的唯一方法,而且它们比静态的元素集合要灵活得多。 A storyboard is essentially a collection of connected scenes, where each scene is a template for a screenful of content.故事板本质上是连接场景的集合,其中每个场景都是一屏内容的模板。 But your code can adjust the objects in a scene, including hiding or showing them, changing their position, and adding ore removing views.但是您的代码可以调整场景中的对象,包括隐藏或显示它们、改变它们的位置以及添加矿石移除视图。 So if you want you could have a scene with a different table for each of the items in your list, and then just adjust the visibility of the various tables according to whatever thing is selected.因此,如果您愿意,您可以为列表中的每个项目创建一个带有不同表格的场景,然后根据选择的任何内容调整各个表格的可见性。 Or you could have no tables at all, and your code could create one on the fly and insert it into the view.或者您可能根本没有表,您的代码可以动态创建一个表并将其插入到视图中。

In short: you're assuming too much about how storyboards work.简而言之:您对故事板的工作方式假设得太多了。 Start reading about how view controllers work and you'll soon have a much better understanding of the kind of control you have over the UI.开始阅读视图控制器的工作原理,您很快就会更好地了解您对 UI 的控制类型。

Let's say I've got an array of arrays and for each array in the array I want to render a tableview.假设我有一个数组数组,对于数组中的每个数组,我想呈现一个 tableview。 How would I approach this problem?我将如何解决这个问题?

You would write code—presumably, Swift code, since you used the tag.您将编写代码——大概是 Swift 代码,因为您使用了标签。

Let's make it even more concrete.让我们让它更具体。 You have a Bookcase object which has an array of Shelf , and each Shelf has an array of Book .你有一个Bookcase对象,它有一个Shelf数组,每个Shelf都有一个Book数组。

If you're using a storyboard, you can have a ShelfController scene in your storyboard with a single table view to display the Book s in one Shelf .如果您使用的是情节ShelfController ,则可以在情节ShelfController中使用ShelfController场景,并使用单个表视图在一个Shelf显示Book You can also have a BookcaseController scene with (let's say) a stack view.你也可以有一个BookcaseController场景(比如)一个堆栈视图。 The stack view has no subviews because you don't know, when designing the storyboard, how many shelves you'll need to display.堆栈视图没有子视图,因为在设计故事板时,您不知道需要显示多少个书架。

Then, in Swift code, when BookcaseController is given its Bookcase model object, the BookcaseController instantiates the ShelfController scene once for each Shelf in the Bookcase .然后,在斯威夫特的代码,当BookcaseController给出了Bookcase模型对象,该BookcaseController实例化ShelfController现场一次为每个ShelfBookcase It adds each ShelfController as a child view controller, and adds each ShelfController 's view (a table view) as an arranged subview of its (the BookcaseController 's) stack view.它将每个ShelfController添加为子视图控制器,并将每个ShelfController的视图(表视图)添加为其( BookcaseController的)堆栈视图的排列子视图。

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

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