UITableView in Swift

前端 未结 17 1415
滥情空心
滥情空心 2020-12-04 08:09

I\'m struggling to figure out what\'s wrong with this code snippet. This is currently working in Objective-C, but in Swift this just crashes on the first line of the method.

17条回答
  •  悲哀的现实
    2020-12-04 08:15

    I went through your codes and most probably the reason for the crash is you are trying to typecast an optional value which is not assigned

    Now consider the line of code below

    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    

    When there are no cells in the tableview you are still trying to typecast as UITableView.When the compiler tries to typecast nil value you face this issue

    The correct statement should be

    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") 
    

    You can use if else statement to typecast for values which holds

提交回复
热议问题