I am facing some weird problems. Whenever I scroll my table view, my data gets replaced with other cells. Each time, it gets replaced with different cell data. I am not seeing any particular pattern in this replacement.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
If you've created a custom cell, you may have to implement
prepareForReuse
on yourUITableViewCell
subclass to clear out cell data.I had the same issue on iOS 7 and storyboard while I never had this problem with the same code on elder Xcode-SDK versions, and trying lots of suggestions from stackoverflow, I couldn't fix it, and guess what?! Apple's TableView Programming Guide has the well-explained guide. There's even no need to use
cell==nil
.So to create a custom tableview cell using storyboard that would be reused properly, you can take two different approacesh. I personally prefer the first one which is more straightforward, but I put both solutions here.
First Approach
dequeueReusableCellWithIdentifier:
message.An important attribute to set for the programmatic portion of this procedure is each object’s
tag
property. Find this property in theView
section of theAttributes inspector
and assign each object a unique integer.Now write the code you would normally write to obtain the table view’s data. (For this example, the only data you need is the row number of each cell.) Implement the data source method tableView:cellForRowAtIndexPath: to create a new cell from the prototype and populate it with data, in a manner similar to this code:
There are a few aspects of this code to note:
dequeueReusableCellWithIdentifier:
.dequeueReusableCellWithIdentifier:
method always returns a valid cell. You don’t need to check the return value against nil and create a cell manually.viewWithTag:
, passing in their tag integers. It can then set the textual content of the labels.Second Approach
If you prefer not to use tags, you can use an alternative method for setting the content in the cell. Define a custom
UITableViewCell
subclass with outlet properties for the objects you want to set. In the storyboard, associate the new class with the prototype cell and connect the outlets to the corresponding objects in the cell.To use outlets for the custom cell content
Add the following code to the interface in
MyTableViewCell.h
:@interface MyTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *firstLabel; @property (nonatomic, weak) IBOutlet UILabel *secondLabel; @end
Add the following code to the implementation in
MyTableViewCell.m
:@synthesize firstLabel, secondLabel;
Add the following line of code to the source file that implements the data source:
#import "MyTableViewCell.h"
Use the
Identity inspector
to set the Class of the prototype cell toMyTableViewCell
.Use the Connections inspector to connect the two outlets in the prototype cell to their corresponding labels.![enter image description here](https://i.stack.imgur.com/nAloO.jpg)
Implement the data source method
tableView:cellForRowAtIndexPath:
The code gains access to the labels in the cell using accessor methods (dot notation is used here). The code can then set the textual content of the labels.
Here's code for how to properly reuse a cell:
If you provide your code we could modify that instead of giving you generic examples.
Old, but still... I've encountered this problem. If others sees this problem one day. I didn't notice, and don't remember why I did that, but I wrote the configuration of the cell INSIDE the test
if (cell == nil)
instead of writing it AFTER. I must have been tired this day...