I'm having issues getting customs cells to show up. Below I've posted a simplified version of my code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
NSLog(#"Being Called?");
//reuseID = #"newtableid";
self.backgroundColor = [UIColor redColor];
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
self.name.backgroundColor = [UIColor redColor];
[self addSubview:self.name];
return self;
}
In my viewController, I set up the tableView in the ViewDidLoad method:
self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 400) style:UITableViewStylePlain];
self.table.delegate = self;
self.table.dataSource = self;
self.table.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.table];
[self.table registerClass:NewTableViewCell.class forCellReuseIdentifier:#"Cell"];
Then, I complete the cellForRowAt method like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[NewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.backgroundColor = [UIColor blackColor];
cell.name.text = [_familyDetails objectAtIndex:indexPath.row];
return cell;
}
The problem is, I don't get anything to show up on my cells. My NewTableVIewCell constructor is called, but the cells show up blank (without color, label, etc.) What am I doing wrong?
If you're creating the cell programatically, make sure you register your cell to your table view.
[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:"yourCellId"];
I am using a generic UITableViewCell in this case. You can try to change the text label of it in cellForRowAtIndexPath to see whether that works.
Be sure to change it to your custom cell class if needed.
In order to dequeueReusableCellWithIdentifier, you have to use
- (void)registerClass:(Class)cellClass
forCellReuseIdentifier:(NSString *)identifier
and normally you will not even have to instantiate NewTableViewCell if you use dequeueReusableCellWithIdentifier:forIndexPath:. However, if you still use dequeueReusableCellWithIdentifier: then you still need to instantiate your cell subclass (thanks for reminding me Alejandro Ivan).
EDIT: So you have to write something like this, in your viewDidLoad for example :
- (void)viewDidLoad {
[super viewDidLoad];
// Your stuff...
[self.table registerClass:[NewTableViewCell class] forCellReuseIdentifier: simpleTableIdentifier];
}
Related
it says
Assigning to 'id _Nullable' from incompatible
type 'ChildTableViewCell *__strong'
is there any mistake if i implement UITableView cell inside custom UITableViewCell ?
So first in MainViewController, Using MainView that have UITableView,
in the MainViewController, especially in ViewDidLoad, i register self.mainView.tableView.delegate = self; also self.mainView.dataSource = self;
and in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath i have this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.filteredCategoriesAllListLocalDataArray count] > 0) {
static NSString *cellID = #"ChildTableViewCell";
ChildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(nil == cell) {
cell = [[ChildTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellID];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
CategoryModel *currentCategory = [self.filteredCategoriesAllListLocalDataArray objectAtIndex:indexPath.section];
ChildModel *currentChildData = [currentCategory.childModelArray objectAtIndex:indexPath.row];
NSLog(#"================== %#", currentChildData.nameString);
cell.childData = currentChildData;
[cell initializeChildTableView];
cell.clipsToBounds = YES;
return cell;
}
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}
and then in ChildTableViewCell i have this:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth([UIScreen
mainScreen].bounds), CGRectGetHeight(self.contentView.frame))style:UITableViewStyleGrouped];
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.showsHorizontalScrollIndicator = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.scrollEnabled = NO;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.contentView addSubview:self.tableView];
}
return self;
}
but in step 4, got this error message:
Assigning to 'id _Nullable' from incompatible
type 'ChildTableViewCell *__strong'
im Using UITableView inside UItableViewCell cause want to make UITAbleView Tree Level with custom data, i've been search all reference for UITableView Multi level but it doesn't help at all.
So I tried subclassing UITableViewCell to make a message cell, I've been testing it and I can't seem to get it to show anything. It's not the data source because it was working fine with I wasn't trying to use a custom cell. I'm not sure why nothing is appearing in the cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MessageCell * cell = (MessageCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil){
cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.messageContent.text = self.messages[indexPath.row][#"content"];
return (UITableViewCell*) cell;
}
this is the init method i have in the subclass of UITableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
//I played with the CGRect numbers but that's not it.
messageContent = [[UILabel alloc]initWithFrame:CGRectMake(3,35, 200,30)];
messageContent.textColor = [UIColor redColor];
messageContent.backgroundColor = [UIColor whiteColor];
messageContent.highlightedTextColor = [UIColor clearColor];
messageContent.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:messageContent];
}
return self
}
1st check using below line :
cell.messageContent.text = #"asdfasdf";
If it's work properly than something wrong in
self.messages[indexPath.row][#"content"]
line . . .
If you are using storyboard, the method -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; most definitely wont get called.
Add this method to MessageCell.h:
- (void)setupCellWithText:(NSString *)text
Add the implementation to MessageCell.m:
- (void)setupCellWithText:(NSString *)text{
messageContent = [[UILabel alloc]initWithFrame:CGRectMake(3,35, 200,30)];
messageContent.textColor = [UIColor redColor];
messageContent.backgroundColor = [UIColor whiteColor];
messageContent.highlightedTextColor = [UIColor clearColor];
messageContent.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:messageContent];
messageContent.text = text;
}
Now modify your delegate method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MessageCell * cell = (MessageCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil){
cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
[cell setupWithText:[self.messages[indexPath.row][#"content"]]];
return (UITableViewCell*) cell;
}
Hope this helps. Sorry for typos.
When you create a custom UITableViewCell your tableView needs to know about it. Here's how. This goes in the UITableViewController's viewDidLoad:
[self.tableView registerClass:[MessageCell class] forCellWithReuseIdentifier:#"cell"];
Now your tableViews knows about your class and no more casting is necessary.
Bonus tip! You don't need this:
if(cell == nil){
cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
dequeueReusableCellWithIdentifier already does that for you.
Here's what the whole thing would look like.
- (void)viewDidLoad {
[super viewDidLoad];
// Register custom cell for table view
[self.tableView registerClass:[MessageCell class] forCellWithReuseIdentifier:#"cell"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MessageCell * cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.messageContent.text = self.messages[indexPath.row][#"content"];
return cell;
}
** I just wrote this in the browser so forgive my errors
Check set your UITableViewDelegate and UITableViewDataSource.
On Xib: See image below.
Or in ViewDidLoad:
self.mTableView.delegate = self;
self.mTableView.dataSource = self;
Hope this helps!
I'm trying to add a UITableView to an existing UIViewController programmatically.
My run.h file has this:
#interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;}
#property (strong, nonatomic) UITableView *tableView;
My app runs through a speed test and at the end of the test I'm trying to add a TableView to the View. At the moment I have this:
-(void)addSpeedTable {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 45;
self.tableView.sectionFooterHeight = 22;
self.tableView.sectionHeaderHeight = 22;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"newCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Testing";
return cell;
}
The app never makes it into the cellForRowAtIndexPath method but does make it into the other two above. I can't see any error in the Xcode output.
Anything I need to be doing here:
When the app fails all I get is this:
If width of UITableView is zero, it will never call datasource
So change your code like this
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
Good luck!
You should init your UITableView with a frame (and optionally a style), or at least define the frame somewhere.
For example:
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];
You need to assign the property tableView to the instance you are creating in the addSpeedTable method, then reference it using self.tableView.
-(void)addSpeedTable {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 45;
self.tableView.sectionFooterHeight = 22;
self.tableView.sectionHeaderHeight = 22;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
Edit
As pointed out by the other answers, you'll probably need to set the frame on the UITableView as well:
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
You use init method to initialize a view, which means the view's frame is CGRectZero.
So the numberOfRowsInSection is called but SDK finds no cell need to be shown as the frame is CGRectZero.
-(void)addSpeedTable {
.....
tableView.frame = self.view.bounds;
[self.view addSubview:tableView];
}
What's more you should assign your tableView to the property by self.tableView = tableView or your tableView property will never be valid state.
You can add breakpoint at the thirdLine, and print out the frame see if one of its bounds is 0.
I think you may have declare the wrong syntax. I can't upload image, So notice the difference the code below. The capitalization may be the problem.
{
// 服务条款
static NSString * cellId = #"justCommon";
static NSString *CellIdentifier = #"newCell";
}
I have a custom cell in a UITableView. I programatically created a label with a height of 200 and a width of 50. When I do an NSLog in the customCell.m of the label's width and height, it gives me w: 50 and h: 200. But when I do an NSLog in mainViewController.m it gives me 0 for the height and width.
Not sure why it does that. I need to get the real height of the label in the mainViewController.m
Here's my code:
customCell.m
- (void)awakeFromNib {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:#"Hello"];
[self.myView addSubview:self.label];
CGRect myFrame = self.myView.frame;
myFrame.size.height = self.label.frame.size.height;
self.myView.frame = myFrame;
NSLog(#"%f", self.label.frame.size.height); // Results: 200.0000
}
mainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
customCell *cellVC = [[cutsomCell alloc] init];
NSLog(#"%f, %f", cellVC.label.frame.size.height, cellVC.frame.size.height); // Results: 0.0000, 44.0000
}
I setmyViewto 0 in the storyBoard.
If you don't understand something, please ask and I will be happy to explain.
Update
Here is my code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:#"Hello"];
[self.myView addSubview:self.label];
view
}
return self;
}
viewDidLoad
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:#"cellID"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCell = #"customCell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID" forIndexPath:indexPath];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:customCell owner:nil options:nil] objectAtIndex:0];
}
return cell;
}
The problem now is that when I run the app, no cells get shown. But I do get the correct NSLog.
In your mainViewController you just programmatically created the cellVC using alloc and then init, you did not create it from the nib, like you did it in the customCell.m so the awakeFromNib method was not called on it.
Update
Change back to using the custom cell from xib (instead of registerCell)
[self.tableView registerNib:[UINib nibWithNibName:#"CustomCellxib"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:#"CustomCellReuseID"]; // in the viewDidLoad of the mainViewController
Then deque the cell - you do not really need to check if the cell is nil because dequeue is now guaranteed to return a cell
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"customCellReuseID" forIndexPath:indexPath];
in your tableview datasource method
Keep the code currently in your awakeFromNib to initialize the custom cell added from nib. The default cell from the xib is fully initialized here so you should be able to customize the custom view. You do not need the initWithIdentifer anymore
You can optionally implement the table view delegate methods like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// calculate the row height or return a constant
return 200.0;
}
The problem is that -awakeFromNib does not get called directly after -init
Try this:
- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setBackgroundColor:[UIColor yellowColor]];
[self.label setText:#"Hello"];
[self.myView addSubview:self.label];
CGRect myFrame = self.myView.frame;
myFrame.size.height = self.label.frame.size.height;
self.myView.frame = myFrame;
NSLog(#"%f", self.label.frame.size.height); // Results: 200.0000
return self;
}
I've got a table view where the cells are created as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"TempTitle";
cell.textLabel.font = [UIFont fontWithName: #"AppleSDGothicNeo-Bold" size:16.0];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UILabel *numberLabel = [[UILabel alloc] init];
[numberLabel setFrame:CGRectMake(230.0, 5.0, 40.0, 30.0)];
[numberLabel setText:[NSString stringWithFormat:#"%#", [self.arrayOne objectAtIndex:indexPath.row]]];
UIButton *buttonDown = [[UIButton alloc] init];
[buttonDown setFrame:CGRectMake(190.0, 5.0, 40.0, 30.0)];
buttonDown.tag = indexPath.row;
[buttonDown addTarget:self action:#selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonUp = [[UIButton alloc] init];
[buttonUp setFrame:CGRectMake(270.0, 5.0, 40.0, 30.0)];
[cell addSubview:buttonDown];
[cell addSubview:numberLabel];
[cell addSubview:buttonUp];
return cell;
}
where self.arrayOne (name changed for this thread) holds integer values that are displayed in each cell. The method when buttonDown is selected is as following:
- (void) quantityDown:(id)sender {
int clicked = ((UIButton *)sender).tag;
... math to lower int by one and save the new value in arrayOne
... this part works just fine. The value does go down, as intended.
[self.tableView reloadData];
}
When the tableView reloads, a new label is printed on top of the existing label in that cell, as can be seen in the image below:
I can only assume that new buttons are being printed on top of the existing ones as well. This is both expensive and makes the numbers unreadable (especially if you change it multiple times!). Leaving the view and coming back to it shows the new numbers cleanly, but only until you start changing them again.
A similar effect happens when I use the UISegmentedControl at the top. Selecting one or the other changes the contents of the table and runs [self.tableView reloadData]. The textLabel for each cell reloads just fine when this method is called, but the sub views do not reload, and instead stack upon one another.
How would I go about writing this so that there is only ever one subview in each cell, instead of multiple stacked upon one another? Something speedy and not expensive on resources. Maybe removing all subviews and then adding the new ones? I tried something like
[[cell.contentView viewWithTag:tagVariable]removeFromSuperview];
to no avail. Really, I only need to modify the one cell in the table that the user is clicking in. Except for when the user uses the UISegmentedControl at the top, then all the cells need to be modified.
Thanks in advance.
EDIT 1:
Created a custom UITableViewCell class...
#interface SSCustomTableViewCell : UITableViewCell
#property (nonatomic) UILabel *quantity;
#property (nonatomic) UIButton *down;
#property (nonatomic) UIButton *up;
#end
#implementation SSWeightsTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.quantity = [UILabel new];
[self.contentView addSubview:self.quantity];
self.down = [UIButton new];
[self.contentView addSubview:self.down];
self.up = [UIButton new];
[self.contentView addSubview:self.up];
}
return self;
}
- (void) layoutSubviews {
[super layoutSubviews];
[self.down setFrame:CGRectMake(190.0, 5.0, 40.0, 30.0)];
[self.down setBackgroundColor:[UIColor purpleColor]];
[self.up setFrame:CGRectMake(270.0, 5.0, 40.0, 30.0)];
[self.up setBackgroundColor:[UIColor purpleColor]];
[self.quantity setFrame:CGRectMake(230.0, 5.0, 40.0, 30.0)];
[self.quantity setTintColor:[UIColor purpleColor]];
}
#end
and then in my UITableView class...
#import "SSCustomTableViewCell.h"
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView
registerClass:[SSCustomTableViewCell class]
forCellReuseIdentifier:NSStringFromClass([SSCustomTableViewCell class])
];
}
and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier: NSStringFromClass([SSWeightsTableViewCell class])
forIndexPath:indexPath
];
}
but now all I see is the following...
where the subviews are not correctly laid out, nor are they the correct size. Also, within the cellForRowAtIndexPath method, I cannot access the cell's components. When I enter
cell.quantity
I get an error saying that "Property 'quantity' not found on object of type UITableViewCell*"
So I tried
SSCustomTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier: NSStringFromClass([SSWeightsTableViewCell class])
forIndexPath:indexPath
];
and the error goes away, but it still looks the same when I run it.
Edit 2: Working Solution
All I had to do was add
cell = [[UITableViewCell alloc]init];
within the cellForRowAtIndexPath method. Everything else stayed the same, and no custom classes required.
You are seeing this error because cell instances are reused. If you add a view each time you configure the cell, you will be adding to cells that already have the subviews on them. You should create your own subclass of UITableViewCell and add the subviews to it in init. Then your method above would just set the values on the various subviews.
Your cell would look something like this:
#interface MyCustomCell : UITableViewCell
#property (nonatomic, strong) UILabel *myCustomLabel;
#end
#implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.myCustomLabel = [UILabel new];
[self.contentView addSubview:self.myCustomLabel];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// Arrange self.myCustomLabel how you want
}
#end
Then your view controller would look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView
registerClass:[MyCustomCell class]
forCellReuseIdentifier:NSStringFromClass([MyCustomCell class])
];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:NSStringFromClass([MyCustomCell class])
forIndexPath:indexPath
];
cell.myCustomLabel.text = #"blah";
return cell;
}
Try this in cell configure method, this will make sure objects doesn't overlap
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
return cell;
}
Hope this will help