tableview's uitableviewcell display in worng order and duplicate rows - uitableview

This is a chat app, I'm using tableview to display the chatting data between user and others, however I'v got Rows are displayed multiple(duplicate). The code is below:
#define kMyTag 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *messageDic = [self.chatArray objectAtIndex:indexPath.row];
if ([[messageDic objectForKey:#"myself"]boolValue] == false) {
static NSString *CellIdentifier = #"MessageChatFriendCell";
MessageChatFriendCell *cell = (MessageChatFriendCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MessageChatFriendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *chatInfo = [self.chatArray objectAtIndex:[indexPath row]];
UIView *chatView = [chatInfo objectForKey:#"view"];
chatView.tag = kMyTag;
[cell.contentView addSubview:chatView];
return cell;
} else {
static NSString *CellIdentifier = #"MessageChatSelfCell";
MessageChatSelfCell *cell = (MessageChatSelfCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MessageChatSelfCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *chatInfo = [self.chatArray objectAtIndex:[indexPath row]];
UIView *chatView = [chatInfo objectForKey:#"view"];
chatView.tag = kMyTag;
[cell.contentView addSubview:chatView];
return cell;
}
}
the print results for each row are:
2012-08-02 15:44:47.152 MessageChatSelfCell-><UIView: 0x222790; frame = (85 0; 230 114); tag = 1; layer = <CALayer: 0x222320>>
2012-08-02 15:44:47.166 MessageChatFriendCell-><UIView: 0x21d790; frame = (0 0; 210 132); tag = 1; layer = <CALayer: 0x21d5c0>>
2012-08-02 15:44:47.176 MessageChatFriendCell-><UIView: 0x21bde0; frame = (0 0; 177 60); tag = 1; layer = <CALayer: 0x21be10>>
2012-08-02 15:44:47.183 MessageChatSelfCell-><UIView: 0x216430; frame = (85 0; 230 168); tag = 1; layer = <CALayer: 0x215fc0>>
2012-08-02 15:44:59.232 MessageChatFriendCell-><UIView: 0x215150; frame = (0 0; 86 60); tag = 1; layer = <CALayer: 0x214eb0>>
2012-08-02 15:45:00.465 MessageChatSelfCell-><UIView: 0x213220; frame = (85 0; 230 78); tag = 1; layer = <CALayer: 0x213250>>
Please advise, thanks!
Updated 2012-08-08
#define WRITER_NAME_LABEL_TAG 4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MessageChatSelfCellIdentifier = #"MessageChatSelfCell";
static NSString *MessageChatFriendCellIdentifier = #"MessageChatFriendCell";
UITableViewCell *cell = nil;
UIView *chatView = nil;
NSDictionary *messageDic = [self.chatArray objectAtIndex:indexPath.row];
NSString *text = [messageDic objectForKey:#"compiled"];
BOOL myMessage = [[messageDic objectForKey:#"myself"] boolValue];
if (myMessage) {
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatSelfCellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatSelfCellIdentifier];
chatView = [self bubbleView:[NSString stringWithFormat:#"%#", text]
from:YES];
chatView.tag = WRITER_NAME_LABEL_TAG;
NSLog(#"MessageChatFriendCell->%#",chatView);
[cell addSubview:chatView];
}else {
chatView = (UIView *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatFriendCellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatFriendCellIdentifier];
NSString *text = [messageDic objectForKey:#"compiled"];
UIView *chatView = [self bubbleView:[NSString stringWithFormat:#"%#", text]
from:NO];
chatView.tag = WRITER_NAME_LABEL_TAG;
NSLog(#"MessageChatSelfCell->%#",chatView);
[cell addSubview:chatView];
}else {
chatView = (UIView *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
}
return cell;
}

I answered your another question How to calculate the Width and Height of NSString on UILabel
They are similar problem. the code below I C&P from the other question.
That's because you did not reuse the table cell, the structure should be like:
NSString *text = [messageInfo objectForKey:#"compiled"];
if(cell == nil)
{
writerNameLabel.numberOfLines = 0;
writerNameLabel.textAlignment = UITextAlignmentRight;
writerNameLabel.backgroundColor = [UIColor clearColor];
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
CGSize constraint = CGSizeMake(296,9999);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeWordWrap];
[writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)];
I've been gone through and answered some of your question, that's correct way to write your tableview controller. And your problem will be solved.

Few things u should change, first don't store your views in chatInfo instead store your data.
Next add the view to the cell only when u create new cell, otherwise u will add more then one view to a reused cell.
Take a look at the example below, here I'm creating a cell with the message writer name, for my messages the writer name will be in the right hand side, and for my friend messages it will be on the left.
Pay attention that I'm only add the writer name label when I create a new cell, then I give it a tag so I can get the label next time I want to change it.
#define WRITER_NAME_LABEL_TAG 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MessageChatFriendCellIdentifier = #"MessageChatFriendCell";
static NSString *MessageChatSelfCellIdentifier = #"MessageChatSelfCell";
UITableViewCell *cell = nil;
UILabel *writerNameLabel = nil;
NSDictionary *messageInfo = [self.chatArray objectAtIndex:indexPath.row];
BOOL myMessage = [[messageInfo objectForKey:#"myself"] boolValue];
if (myMessage) {
// this is my message
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatSelfCellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatSelfCellIdentifier] autorelease];
writerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
writerNameLabel.tag = WRITER_NAME_LABEL_TAG;
writerNameLabel.textAlignment = UITextAlignmentRight;
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
}
else {
// this is my friend message
cell = [tableView dequeueReusableCellWithIdentifier:MessageChatFriendCellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MessageChatFriendCellIdentifier] autorelease];
writerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
writerNameLabel.tag = WRITER_NAME_LABEL_TAG;
writerNameLabel.textAlignment = UITextAlignmentLeft;
[cell addSubview:writerNameLabel];
}
else {
writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
}
// get the current writer name from the model
NSString *writerName = [messageInfo objectForKey:#"writer"];
// configure cell...
writerNameLabel.text = writerName;
return cell;
}

Related

UITableViewCell label resizes when I scroll

I'm trying to create a standings table with a UITableView.
I've created a custom cell, with 7 labels. I have used grouped cells to differentiate between AL Central, NL East, etc.
Check out this image, when I load the view:
Compare that with the image when I scroll the view:
Here is my code (removed the repeated code):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}// end
//rank
UILabel *rank = (UILabel *)[cell viewWithTag:101];
//team
UILabel *team = (UILabel *)[cell viewWithTag:102];
if(indexPath.row == 0)
{
NSArray *division = #[#"AL Central", #"AL East", #"AL West", #"NL Central", #"NL East", #"NL West"];
rank.text = #"";
team.text = division[indexPath.section];
cell.backgroundColor = textPrimary;
team.textColor = backgroundPrimary;
team.font = [UIFont boldSystemFontOfSize:5.0f];
won.font = [UIFont boldSystemFontOfSize:11.0f];
return cell;
}
cell.backgroundColor = [UIColor clearColor];
rank.textColor = textPrimary;
team.textColor = textPrimary;
int new_row = indexPath.row + indexPath.section*5 - 1;
rank.text = [[[dataSource objectAtIndex:new_row] objectForKey:#"rank"] stringValue];
team.text = [[dataSource objectAtIndex:new_row] objectForKey:#"first_name"];
return cell;
}
I solved this problem by setting the font again when it wasn't the header:
team.font = [UIFont systemFontOfSize:5.0f];

UItableVIew wrong layout

I have a ViewController with three TableView like the following screenshot:
This is the declaration code of three tableview:
CGFloat x, y, w, h;
x = 10.0f;
y = 154.0f;
w = 220.0f;
h = 600.0f;
self.usersTableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:UITableViewStylePlain];
self.usersTableView.delegate = self;
self.usersTableView.dataSource = self;
[self.view addSubview:self.usersTableView];
x = CGRectGetMaxX(self.usersTableView.frame) + 10.0f;
self.groupsTableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:UITableViewStylePlain];
self.groupsTableView.delegate = self;
self.groupsTableView.dataSource = self;
[self.view addSubview:self.groupsTableView];
x = CGRectGetMaxX(self.groupsTableView.frame) + 10.0f;
self.officesTableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:UITableViewStylePlain];
self.officesTableView.delegate = self;
self.officesTableView.dataSource = self;
[self.view addSubview:self.officesTableView];
The first table has a different content layout. I've try to change the order of the three tables but the first table shows always a wrong layout (I hide the content of the row for privacy).
EDIT
Here the data source methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.usersTableView)
{
return self.usersDataSource.count;
}
else if (tableView == self.groupsTableView)
{
return self.groupsDataSource.count;
}
else if (tableView == self.officesTableView)
{
return self.officesDataSource.count;
}
return 0;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.usersTableView)
{
static NSString *CellIdentifier = #"Cell_User";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
}
User * aUser = self.usersDataSource[indexPath.row];
cell.textLabel.text = aUser.Name;
return cell;
}
else if (tableView == self.groupsTableView)
{
static NSString *CellIdentifier = #"Cell_Group";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
}
Group * aGroup = self.groupsDataSource[indexPath.row];
cell.textLabel.text = aGroup.Name;
return cell;
}
else if (tableView == self.officesTableView)
{
static NSString *CellIdentifier = #"Cell_Office";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
}
Office * anOffice = self.officesDataSource[indexPath.row];
cell.textLabel.text = anOffice.Name;
return cell;
}
return nil;
}
Thank you very much.
In iOS 7 the UIViewController automatically insets the first UIScrollView it can find in the view hierarchy to account for the new translucent UINavigationBar.
To turn of this behavior try to change the 'automaticallyAdjustsScrollViewInsets' property on your UIViewController to 'NO' in the 'init' method.
self.automaticallyAdjustsScrollViewInsets = NO;
More info can be found here:
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/transitionguide/AppearanceCustomization.html
If that does not help, there must be something wrong with your UITableViewDataSource.

My UITableViewCell is reusing a previous cells UIImage

I have a UITable that reuses cells. Each cell has an image dependant on a condition. The correct images are always used however when cells are reused the old image is displayed behind the new one. I have tried to remove the uiimageview before adding a new subview however it has no effect.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
UIImageView *achievImg = [[UIImageView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
[achievImg removeFromSuperview];
[cell.contentView addSubview:achievImg];
return cell;
}
Any help would be greatly appreciated?
The problem is each time cellForRowAtIndexPath: is called you create a new imageview.
I corrected your code as follows:
Here we create an imageView once for each new new cell and replace it's image every time cellForRowAtIndexPath: is called:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
UIImageView *achievImg = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
//New Cell ... Create Image view
achievImg = [[UIImageView alloc] init];
achievImg.tag = 10;
[cell.contentView addSubview:achievImg];
}
else{
//Old cell...get previously createdd imageview
achievImg = (UIImageView*)[cell.contentView viewWithTag:10];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
//You don't need this because it doesn't have a superview in the first place..it's new
//[achievImg removeFromSuperview];
return cell;
}
try like this,
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
UIImageView *achievImg = [[UIImageView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
[achievImg removeFromSuperview];
[cell.contentView addSubview:achievImg];
}
return cell;
}
You are always adding the imageView to the contentView of the cell. If the cell is reused that will lead to multiple imageViews in cell.
You can give a tag to the imageView. Always check if there is a view will same tag in the contentView of cell. If not add the imageView.
UIImageView *achievImg = (UIImageView *)[cell.contentView viewWithTag:55];
if(!achievImg){
achievImg = [[UIImageView alloc] init];
achievImg.tag = 55;
[cell.contentView addSubView:achievImg];
}

custom cell data not displayed ios

I need to display the cell data. Actually if I debug the code I can see that the cell.label.text does have correct value, but nothing is getting displayed on the simulator. I have set the row count in numberOfRowsInSection method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
LogTableCell *cell = (LogTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
CGRect frame1 = [ self.view frame];
frame1.origin.x=22.0f;
frame1.origin.y=55.0f;
frame1.size.width = 298.0f;
frame1.size.height= 425.0f;
[self.view.superview setFrame:frame1];
int btnWidth = ((self.view.frame.size.width-16)/3);
if(cell == nil)
{
cell = [[LogTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
LogItem *lg = [[LogItem alloc]init];
lg = [arrLogs objectAtIndex:indexPath.row ];
NSLog(#"%d",indexPath.row);
cell.lblname.textColor=[UIColor whiteColor];
cell.lblsubtype.textColor=[UIColor whiteColor];
cell.lbltime.textColor=[UIColor whiteColor];
cell.lblname.font=[UIFont fontWithName:#"Merriweather" size:15.0];
cell.lblsubtype.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
cell.lbltime.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(1, cell.contentView.frame.size.height, 298.0,2)] ;
imageview.image = [UIImage imageNamed: #"lineseperator.png"];
[cell.contentView addSubview:imageview];
cell.lblname.text = lg.name;
NSLog(#"%#",lg.name);
[[cell imgtype] setImage:[UIImage imageNamed:lg.imagetype]];
if([lg.subtype length] == 0){
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.time;
cell.lbltime.text = #"";
}
else{
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.subtype;
cell.lbltime.text = lg.time;
}
return cell;
}
The imageview you're adding to the cell is covering all other content. You'll need to either resize/reposition it, or just use the already existing cell.imageView instead.

UITableView list not displaying all items

In my UITableView I am trying to display all the items in my plist but its not showing all the items. Actually it is showing most of it but the lower items are being repeated for some odd reason. I basically want to show all the keys in the plist with their respective values. Is the list too long to display? there's about 30 items.
First I tried to sort the keys and thought that was the problem, so then I didn't sort at all and I get the same problem, lower down the list the items get repeated and not showing the last 3 items. Is there a limit?
Below is some code, I've just modified to fit:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"PreferencesCell1";
static NSString *CellIdentifier2 = #"PreferencesCell2";
static NSString *CellIdentifier3 = #"PreferencesCell3";
UITableViewCell *cell;
NSArray *keys = [[[preferences objectAtIndex:indexPath.section] objectForKey:#"Rows"] allKeys];
NSString *prefName = [keys objectAtIndex:indexPath.row];
if (indexPath.section == 0 || indexPath.section == 2) {
if(indexPath.section == 0)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
else if(indexPath.section == 2)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
else if(indexPath.section == 2)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *settingName = [[UILabel alloc] initWithFrame:labelRect];
settingName.font = [UIFont boldSystemFontOfSize:17.0];
settingName.backgroundColor = [UIColor clearColor];
settingName.text = prefName;
[cell.contentView addSubview: settingName];
[settingName release];
}
} else if(indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3] autorelease];
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.backgroundColor = [UIColor clearColor];
label.text = prefName;
[cell.contentView addSubview: label];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
What I've found is that if I don't use the labels and just go for the generic cell.textLabel.text approach then all the items are displayed correctly. However if I use the UILabel approach, the bottom items are not shown. I need to go this route as I'm adding other items in the Cell.
Working Code.
Initialization and creation of cell must be created first, then using that referenced cell remove from superview, then render the subviews. So reordering of the code from above.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"PreferencesCell1";
static NSString *CellIdentifier2 = #"PreferencesCell2";
static NSString *CellIdentifier3 = #"PreferencesCell3";
UITableViewCell *cell;
NSArray *keys = [[[preferences objectAtIndex:indexPath.section] objectForKey:#"Rows"] allKeys];
NSString *prefName = [keys objectAtIndex:indexPath.row];
// Create/Initialize Cell first
if (indexPath.section == 0 || indexPath.section == 2) {
if(indexPath.section == 0)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
else if(indexPath.section == 2)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
else if(indexPath.section == 2)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
}
} else if(indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3] autorelease];
}
}
// remove from superview
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// render the subviews
if (indexPath.section == 0 || indexPath.section == 2) {
cell.accessoryType = UITableViewCellAccessoryNone;
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *settingName = [[UILabel alloc] initWithFrame:labelRect];
settingName.font = [UIFont boldSystemFontOfSize:17.0];
settingName.backgroundColor = [UIColor clearColor];
settingName.text = prefName;
[cell.contentView addSubview: settingName];
[settingName release];
} else if(indexPath.section == 1) {
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.backgroundColor = [UIColor clearColor];
label.text = prefName;
[cell.contentView addSubview: label];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
It looks like cells are being reused and you are just adding new views to their existing contents. You need to reset the content, as described here: UITbleViewCell Class Reference. If you were just setting the cell's textLabel each time, setting a new value would suffice here, but if you are adding subviews you may need something more like [cell.contentView.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
The Limit for a Tableview is the free RAM size.
Please post some Code. But i think that this could be a Problem with Cell caching.

Resources