Looking at the official TableView guide today, it suddenly occurred to me that there was a problem when implementing Tableviewcell multiplexing in a recently written code:
var cell == myperipherals[indexpath.row].name
As you can see here, it is definitely not good to create a new cell object every time you call Tableview:cellforrowatindexpath:
The orthodoxy in OC should be implemented in the following way:
First use the dequeuereusablecellwithidentifier to get the cell already in the queue, then take it out and reuse it.
If the cell is nil, re-instantiate a Tableviewcell object, then use ~ ~ ~
So I'm just going to change the swift code that I have in question back, like this:
But this will collapse, whether it is written in the form of optional value:
var cell? = Tableview.dequeuereusablecellwithidentifier(identifer) as ? UITableViewCell
Will report the same mistake, after a long while to find that the original error is not the cell = = nil caused, because "Create new cell" has not been output.
The cause of the error appears on the Cell.detailtextlabel, indicating that the cell has not been set to subtitle style.
To see Apple's official Tableviewsuite Sample Code, the call to Tableviewcell found
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *myidentifier =@"Myidentifier"; /*Retrieve A cell with the given identifier from the table view. The cell is defined in the main storyboard:its identifier are myidentifier, and its selection style are set to None. */UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:myidentifier]; //Set up the cell.NSString *timezonename =[Self.timezonenames ObjectAtIndex:indexPath.row]; Cell.textLabel.text=timeZoneName; returncell;}
This is also a direct call to the Dequeuereusablecellwithidentifier:myidentifier function, which does not instantiate a Tableviewcell object.
Here is a description of this:
/* Retrieve a cell with the given identifier from the table view. The cell is defined in the main storyboard:its identifier are myidentifier, and its selection style are set to None. */
Plus the Apple official TableView Guide document reads:
If the Dequeuereusablecellwithidentifier:method asks for a cell that's defined in a storyboard and the method always returns A valid cell. If There is not a recycled cell waiting to being reused, the method creates a new one using the information in the storyboard itself. This eliminates the need to check the return value for nil and create a cell manually.
is to use the Dequeuereusablecellwithidentifier:myidentifier function will always get a valid cell defined in the storyboard, and use the information defined in storyboard to set the cell , which eliminates the time to detect whether the cell is nil, and then to manually create.
As you can see from here, the error in my code is that the cell is not set to subtitle in storyboard, otherwise there should be no error.
So the following is the simplest way to do this:
" MyCell " as="detail"
However, it is important to note that such a way requires some of the necessary properties of the cell to be set in storyboard.
If so, how can the custom cell be implemented? (To be continued ....) )
Swift Tabeleviewcell Dequeuereusablecellwithidentifier used the new details that turned out now can be so