iOS 개발 학습 노트 2: UItableView(1)

6149 단어 UITableView
1: TableViewController
1: 기본 ViewController를 제거하고 TableViewController를 드래그합니다.
2: TableViewController라는 이름으로 새 Cocoa Touch Class
3:1국을 2로 정하다
4: 레이블을 드래그하고 TAG를 1로 설정하고 CELL의 ID를 cell로 설정합니다.
관련 코드:
 1     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

 2 

 3         return 1

 4     }

 5 

 6     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

 7 

 8         return 4

 9     }

10 

11     

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

13         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

14         

15         var label=cell.viewWithTag(1) as UILabel

16         

17         label.text=""

18        

19         return cell

20     }
2: TableView
1, TableView 드래그
2, 새 Cocoa Touch Class, 이름: My TV
3, 1과 2를 연결하고 Cell을 TableView로 드래그하여 Cell이라고 명명합니다
4, label 드래그, TAG 설정 1
관련 코드:
 1 class MyTV: UITableView,UITableViewDataSource {

 2 

 3     

 4   let data=["  ,     ","didisswfids","w     "]
 5     

 6     required init(coder aDecoder:NSCoder)

 7     {

 8         super.init(coder: aDecoder)

 9         self.dataSource=self

10     }

11     

12     func numberOfSectionsInTableView(tableView: UITableView) -> Int {

13         return 1

14     }

15     

16     

17     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

18         return 4

19     }

20 

21     

22     

23     

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

25         

26 

27         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

28         

29         var label=cell.viewWithTag(1) as UILabel

30         

31         label.text=data[indexPath.row]
32         

33         return cell

34     }

35 }

좋은 웹페이지 즐겨찾기