¼ÓÈëÊÕ²Ø | ÉèΪÊ×Ò³ | »áÔ±ÖÐÐÄ | ÎÒҪͶ¸å Àî´óͬ £¨https://www.lidatong.com.cn/£©- ¿Æ¼¼¡¢½¨Õ¾¡¢¾­Ñé¡¢ÔÆ¼ÆËã¡¢5G¡¢´óÊý¾Ý,Õ¾³¤Íø!
µ±Ç°Î»Ö㺠Ê×Ò³ > °Ù¿Æ > ÕýÎÄ

UITableView, UIPickerViewΪʲôҪʹÓÃdelegateģʽ

·¢²¼Ê±¼ä£º2020-12-14 06:55:52 ËùÊôÀ¸Ä¿£º°Ù¿Æ À´Ô´£ºÍøÂçÕûÀí
µ¼¶Á£ºÎÊÌâ:I get that with delegates you are delegating a task to another object. I guess I just don't understand why this is beneficial. ×î¼Ñ´ð°¸: ¼ò¶øÑÔÖ®,UITableViewרעͨÓù¦ÄܵÄʵÏÖ --Äã²»±Ø¿¼ÂÇÈçºÎ²ÅÄÜÈÃtable view¹ö¶¯,²»±Ø¿¼ÂǼǼÊý¹ý¶àÔõ

ÎÊÌâ:I get that with delegates you are delegating a task to another object. I guess I just don't understand why this is beneficial.

×î¼Ñ´ð°¸:

¼ò¶øÑÔÖ®,UITableViewרעͨÓù¦ÄܵÄʵÏÖ --Äã²»±Ø¿¼ÂÇÈçºÎ²ÅÄÜÈÃtable view¹ö¶¯,²»±Ø¿¼ÂǼǼÊý¹ý¶àÔõô°ì(ÔõÃ´ÖØÓÃcell)²»±ØÅжÏÊÇÄĸöcell±»µã»÷ÁË. UITableView»á¸æËßÄã,ÄãÖ»ÒªÀÏÀÏʵʵµÄʵÏÖËüÖ¸¶¨µÄ½Ó¿Ú,×öÒ»¸öºÏ¸ñµÄdelegate,ʵÏÖÓëUIÎ޹صÄÒµÎñÂß¼­¾ÍÐÐÁË. UITableViewºÍDelegate(ͨ³£ÊÇViewController) ¸÷˾ÆäÖ° ---×öÒ»¼þÊÂ,²¢°ÑËü×öºÃ.

Ô­ÎÄÈçÏÂ:

[¨C]Eoghain4 points13 hours ago

UITableView is the best example to think of here to get your head around it.

What does the UITableView do?

  • Display "cell" objects in a vertical stack
  • Scroll to allow displaying more cells than the visible area could hold
  • Reuse cells so that we don't run out of memory
  • Identify cells that are touched

And other things but that's a good place to start. Now you want to build an application that displays data in cells that can scroll on multiple pages without using delegates how would you create your own UITableView to be able to do this? I'm sure the basics above would be easy to implement and figure out,but what about these questions:

  • Would you pass in the cell object to draw?
  • And the data objects to fill out that cell,(array,dictionary,custom class)?
  • What if you needed to display more than 1 type of cell?
  • How would you deal with the action(s) to perform on a cell when it's tapped?
  • Do all cells perform the same action?

Is the version of UITableVIew that you are building even re-usable at this point? Not really,you'd have so much branching logic that your nice clean re-usable UITableView that just deals with laying things out on screen is a big buggy mess with no easy way to test it.

Now if you just focused only on the core features of what a UITableView does and instead delegated out the application logic to someone else you can see how clean and testable your UITableView becomes. All it cares about is putting some view it's given by someone else on screen at a specific location,allowing the views it's displaying to scroll,and telling whoever cares when a cell get's touched. This version of UITableView can be used by anyone for any purpose to display any data. It doesn't care what that data is,it doesn't care what happens when a cell is touched. It just delegates those decisions to someone else.

What delegates allow you to do is build re-usable code blocks that do "one" thing and do it well. They let the application logic be placed in less generic re-usable objects and allow developers to basically forget about all of the complexity of what the re-usable object is doing. How often do you concern yourself with the scrolling behavior of a UITableView? You don't,you know it'll work and do what it's told.

£¨±à¼­£ºÀî´óͬ£©

¡¾ÉùÃ÷¡¿±¾Õ¾ÄÚÈݾùÀ´×ÔÍøÂ磬ÆäÏà¹ØÑÔÂÛ½ö´ú±í×÷Õ߸öÈ˹۵㣬²»´ú±í±¾Õ¾Á¢³¡¡£ÈôÎÞÒâÇÖ·¸µ½ÄúµÄȨÀû£¬Ç뼰ʱÓëÁªÏµÕ¾³¤É¾³ýÏà¹ØÄÚÈÝ!

    ÍÆ¼öÎÄÕÂ
      ÈȵãÔĶÁ