how to place star rating image next to the textlabel - iphone

i m parsing json feed and according to the parsed value i m placing star images in table view cell.what i want is to place the star rating images as soon as the no of words in the textlabel gets completed the star rating images got to be flexible and not to have fixed frame. below is the code and screenshot of how the output got to look like.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(-18,-2,90,90)] autorelease];
mi.tag = 999;
[cell addSubview:mi];
}
else
{
mi=(HJManagedImageV *)[cell viewWithTag:999];
[mi clear];
}
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:#"%#",boy];
NSInteger n=[str intValue];
NSLog(#"the value:%#",str);
if(n ==0)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"nostar.png"];
starImage.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=1)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"1star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
CGRect labelFrame = CGRectMake(290,18, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
Label.text=str;
Label.tag=1000;
//Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=2)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"twostar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=3)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"threestar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >= 4)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"4star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18,40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
//Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >= 5)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"5star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.font = [UIFont systemFontOfSize:18];
Label.text=str;
Label.tag=1000;
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
cell.textLabel.text=[self.story objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines=2;
return cell;
}

try out this code, i have solved
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(-18,-2,90,90)] autorelease];
mi.tag = 999;
[cell addSubview:mi];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
CGSize sizeTitle = [[self.story objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:16.0]
constrainedToSize:CGSizeMake(200, 40)
lineBreakMode:UILineBreakModeWordWrap];
float width = sizeTitle.width;
NSLog(#"Width %f", width);
width = width+15;
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:#"%#",boy];
NSInteger n=[str intValue];
NSLog(#"the value:%#",str);
CGRect coreFrame;
if(width<170){
coreFrame = CGRectMake(2, 6, width, 40);
}
else{
coreFrame = CGRectMake(2, 6, 170, 40);
width = 175;
}
float k = width+85;
UILabel *lab = [[UILabel alloc] initWithFrame:coreFrame];
lab.text = [self.story objectAtIndex:indexPath.row];
[cell.contentView addSubview:lab];
if(n ==0)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"nostar.png"];
starImage.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
// str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
[cell.contentView addSubview:Label];
}
if(n >=1)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"1star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
CGRect labelFrame = CGRectMake(k,6, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
[cell.contentView addSubview:Label];
}
if(n >=2)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"twostar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(k,6, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=3)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"threestar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
// str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
[cell.contentView addSubview:Label];
}
if(n >= 4)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"4star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
//str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6,40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
//Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >= 5)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"5star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
// str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.font = [UIFont systemFontOfSize:18];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
return cell;
}

You can use the following method to calculate the width and height of a string --
– sizeWithFont:
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
or you can refer the doc here https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
on the basis of width you need to set your label width and then set the frame of your star image.
an example -
CGSize sizeTitle = [titleStr sizeWithFont:[UIFont boldSystemFontOfSize:16.0]
                                      constrainedToSize:CGSizeMake(200, 40)
                                          lineBreakMode:UILineBreakModeWordWrap];
float width = sizeTitle.width;

Related

Tableview cell Repeating the Content on reload tableview and scrolling

My tableview cell content repeats on scrolling and reloading the Data.
I also know that changing the reuse identifier to nill will solve the issue but I need to use the reuse identifier due to the reason mentioned in this link IOS 7 UITextField resignFirstResponder BAD
Is there any other way that i can solve the repeating problem (along with reusing cells)
here is my code
CODE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.textColor = [UIColor myPlaceholderColor];
switch (indexPath.section) {
case 0:{
if (indexPath.row == 0) {
static NSString *ProfileTableIdentifier = #"ProfileCell";
ProfileCell *profilecell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:ProfileTableIdentifier];
if (profilecell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell" owner:self options:nil];
profilecell = [nib objectAtIndex:0];
}
profilecell.photoText.text = #"Photo";
profilecell.photoText.hidden = YES;
profilecell.seperatorView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"TableMiddleBackground.png"]];
profilecell.leftImage1.image = [UIImage imageNamed:#"single tag.png"];
profilecell.leftImage2.image = [UIImage imageNamed:#"tag.png"];
profilecell.profileName.placeholder = #"Profile Name";
profilecell.profileName.tag = 1;
profilecell.profileName.autocapitalizationType = UITextAutocapitalizationTypeNone;
profilecell.userName.placeholder = #"Username";
profilecell.userName.tag = 2;
profilecell.userName.autocapitalizationType = UITextAutocapitalizationTypeNone;
profilecell.avatarImageView.layer.masksToBounds = YES;
profilecell.avatarImageView.layer.cornerRadius = 6.0f;
[profilecell.imageViewTap addTarget:self action:#selector(takePicture:)];
if ([profileName length] != 0 ) {
profilecell.profileName.text = profileName;
}
if ([userName length] != 0 ) {
profilecell.userName.text = userName;
}
if (imageSelected){
profilecell.avatarImageView.image = avatarImage;
}else if ([profileImageUrl length] != 0 ) {
profilecell.avatarImageView.imageURL = [NSURL URLWithString:profileImageUrl];
}else {
profilecell.avatarImageView.image = [UIImage imageNamed:#"adduser.png"];
}
return profilecell;
}else{
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"profile bio.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
profileBioField = [[MyTextView alloc]initWithFrame:CGRectMake(40, 5, 250, MAX(35, profileBioRowHeight))];
profileBioField.placeholder = #"Profile Bio";
profileBioField.backgroundColor = [UIColor clearColor];
profileBioField.font = [UIFont boldSystemFontOfSize:14];
profileBioField.delegate = self;
profileBioField.scrollEnabled = NO;
//profileBioField.editable = NO;
profileBioField.dataDetectorTypes = UIDataDetectorTypeAll;
profileBioField.autocapitalizationType = UITextAutocapitalizationTypeNone;
profileBioField.keyboardType = UIKeyboardTypeASCIICapable;
[cell.contentView addSubview:profileBioField];
if ([profileBio length] != 0 ) {
profileBioField.text = profileBio;
}
return cell;
}
}
break;
case 1:{
if ([fbName length] ==0) {
cell.textLabel.text = #"Use Your account info";
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"arrow.png"]];
}else{
cell.textLabel.text = [NSString stringWithFormat:#"Logged in as %#",name];
cell.accessoryView = nil;
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.textColor = [UIColor myPlaceholderColor];
return cell;
}break;
case 2:{
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"GenderCell";
UITableViewCell *gendercell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (gendercell == nil) {
gendercell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
gendercell.selectionStyle = UITableViewCellSelectionStyleNone;
male = [UIButton buttonWithType:UIButtonTypeCustom];
male.frame = CGRectMake(10, 2, 45, 40);
[male setBackgroundImage:[UIImage imageNamed:#"men in gray.png"] forState:UIControlStateNormal];
[male setBackgroundImage:[UIImage imageNamed:#"men in blue.png"] forState:UIControlStateSelected];
[male addTarget:self action:#selector(maleClicked:) forControlEvents:UIControlEventTouchUpInside];
[gendercell.contentView addSubview:male];
female = [UIButton buttonWithType:UIButtonTypeCustom];
female.frame = CGRectMake(65, 2, 45, 40);
[female setBackgroundImage:[UIImage imageNamed:#"women in gray.png"] forState:UIControlStateNormal];
[female setBackgroundImage:[UIImage imageNamed:#"women in blue.png"] forState:UIControlStateSelected];
[female addTarget:self action:#selector(femaleClicked:) forControlEvents:UIControlEventTouchUpInside];
[gendercell.contentView addSubview:female];
if ([gender isEqualToString: #"male"]) {
[male setSelected:YES];
}else if ([gender isEqualToString: #"female"]){
[female setSelected:YES];
}
return gendercell;
}else if(indexPath.row == 1) {
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"phone.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
phoneNumberField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
phoneNumberField.tag = 3;
phoneNumberField.font = [UIFont boldSystemFontOfSize:14];
phoneNumberField.delegate = self;
[cell.contentView addSubview:phoneNumberField];
phoneNumberField.placeholder = #"Phone Number";
phoneNumberField.keyboardType = UIKeyboardTypePhonePad;
phoneNumberField.textColor = [UIColor darkGrayColor];
phoneNumberField.clearButtonMode = UITextFieldViewModeWhileEditing;
if ([phoneNumber length] != 0) {
phoneNumberField.text = phoneNumber;
}
} else if(indexPath.row == 2) {
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"mail box icon.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
emailIdField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
emailIdField.tag = 4;
emailIdField.font = [UIFont boldSystemFontOfSize:14];
emailIdField.delegate = self;
[cell.contentView addSubview:emailIdField];
emailIdField.placeholder = #"Email Address";
emailIdField.keyboardType = UIKeyboardTypeEmailAddress;
emailIdField.autocapitalizationType = UITextAutocapitalizationTypeNone;
emailIdField.textColor = [UIColor darkGrayColor];
emailIdField.clearButtonMode = UITextFieldViewModeWhileEditing;
emailIdField.autocorrectionType = UITextAutocorrectionTypeNo;
if ([emailId length] != 0) {
emailIdField.text = emailId;
}
} else if(indexPath.row == 3) {
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"location.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
locationField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
locationField.tag = 5;
locationField.font = [UIFont boldSystemFontOfSize:14];
locationField.delegate = self;
[cell.contentView addSubview:locationField];
locationField.placeholder = #"Location";
locationField.textColor = [UIColor darkGrayColor];
locationField.clearButtonMode = UITextFieldViewModeWhileEditing;
if ([location length] != 0) {
locationField.text = location;
}
} else if(indexPath.row == 4) {
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"education.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
educationField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
educationField.tag = 6;
educationField.font = [UIFont boldSystemFontOfSize:14];
educationField.delegate = self;
[cell.contentView addSubview:educationField];
educationField.placeholder = #"Education";
educationField.textColor = [UIColor darkGrayColor];
educationField.clearButtonMode = UITextFieldViewModeWhileEditing;
if ([educationtxt length] != 0) {
educationField.text = educationtxt;
}
} else if(indexPath.row == 5) {
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"birthday.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
birthdayField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
birthdayField.tag = 7;
birthdayField.font = [UIFont boldSystemFontOfSize:14];
birthdayField.delegate = self;
[cell.contentView addSubview:birthdayField];
birthdayField.placeholder = #"Birthday";
birthdayField.textColor = [UIColor darkGrayColor];
birthdayField.clearButtonMode = UITextFieldViewModeWhileEditing;
if ([birthDay length] != 0) {
birthdayField.text = birthDay;
}
}
}
break;
case 3:{
if (indexPath.row == 0) {
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"keynew.png"];
customImage.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:customImage];
passwordField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
passwordField.tag = 8;
passwordField.font = [UIFont boldSystemFontOfSize:14];
passwordField.delegate = self;
[cell.contentView addSubview:passwordField];
passwordField.placeholder = #"Create Password";
passwordField.keyboardType = UIKeyboardTypeASCIICapable;
passwordField.textColor = [UIColor darkGrayColor];
passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordField.secureTextEntry = YES;
if ([password length] != 0 ) {
passwordField.text = password;
}
} else {
}
}break;
case 4:{
UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
customImage.image =[UIImage imageNamed:#"camera.png"];
customImage.contentMode = UIViewContentModeCenter;
//[cell.contentView addSubview:customImage];
cell.imageView.image = [UIImage imageNamed:#"camera.png"];
cell.textLabel.text = #"Enable Camera";
TTSwitch *squareThumbSwitch = [[TTSwitch alloc] initWithFrame:(CGRect){ CGPointZero, { 76.0f, 27.0f } }];
squareThumbSwitch.trackImage = [UIImage imageNamed:#"square-switch-track"];
squareThumbSwitch.overlayImage = [UIImage imageNamed:#"square-switch-overlay"];
squareThumbSwitch.thumbImage = [UIImage imageNamed:#"square-switch-thumb"];
squareThumbSwitch.thumbHighlightImage = [UIImage imageNamed:#"square-switch-thumb-highlight"];
squareThumbSwitch.trackMaskImage = [UIImage imageNamed:#"square-switch-mask"];
squareThumbSwitch.thumbMaskImage = nil; // Set this to nil to override the UIAppearance setting
squareThumbSwitch.thumbInsetX = -3.0f;
squareThumbSwitch.thumbOffsetY = -3.0f; // Set this to -3 to compensate for shadow
[squareThumbSwitch setOn:[UserDefaults privacyStatus]];
[squareThumbSwitch addTarget:self action:#selector(cameraChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = squareThumbSwitch;
return cell;
}break;
default:
break;
}
return cell;
}
Profile Cell is a UITableViewCell. You cant assign it with a view. Instead of using
ProfileCell *profilecell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:ProfileTableIdentifier];
if (profilecell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProfileCell" owner:self options:nil];
profilecell = [nib objectAtIndex:0];
}
Use only one line
[[NSBundle mainBundle] loadNibNamed:#"ProfileCell" owner:self options:nil];

UITableView Scrolling slowness issues when loading cell

I am having issues with the speed in which my tableview Cells scroll. You can see that it hangs up when dequeuing and reusing a cell. Below is my code that I use to create the cells. I am using 2 custom cells and one cell is if there is a image and the other cell is if the user didn't attach an image. Any insight would be greatly appreciated. I should also add that I wanted to add a space between the cells so basically I create a new section containing a single cell and that is why you will always see in my code indexPath.section.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
static NSString *noCell = #"NoCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:noCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
ShameViewCell *cell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[cell.imageView addSubview:iiv];
cell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapImage];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.lastShame setTextColor:insideColor];
[cell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
cell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[cell.shameDate setTextColor:insideColor];
[cell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
cell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.lastShame setBackgroundColor:[UIColor clearColor]];
return cell;
}
}else
{
ShameNoImage *cell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
[cellImageView setBorderWidth:1.0];
[cellImageView setBorderColor:[[UIColor whiteColor] CGColor]];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.shameLabel setTextColor:insideColor];
[cell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
cell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[cell.dateLabel setTextColor:insideColor];
[cell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.shameLabel setBackgroundColor:[UIColor clearColor]];
return cell;
}
}
return cell;
}
The first thing that jumps out are the calls to setCornerRadius. A quick test would be to cut out that corner radius stuff to see if that helps your speed. If that's you're problem you'll have to switch to drawing with CG and a UIBezierPath.
The next thing to look at would be transparency and the number of subviews you are using. It's hard to tell with what is posted above but the more subviews, especially with alpha are going to mean more work compositing. But that can be hard to get around depending on your design but drawing with CG or using images can help.
I figured it out. I was recreating and drawing every cell. I needed to set the reuseIdentifier in the Cell's Subclass and then change the building the the table to be like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
ShameViewCell *iCell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
ShameNoImage *niCell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
if (iCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
iCell = [nib objectAtIndex:0];
CALayer *cellImageView = [iCell.imageView layer];
[cellImageView setMasksToBounds:YES];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[iCell.imageView addGestureRecognizer:tapImage];
[iCell.contentView setBackgroundColor:[UIColor blackColor]];
[iCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
iCell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[iCell.lastShame setTextColor:insideColor];
[iCell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[iCell.shameDate setTextColor:insideColor];
[iCell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[iCell.userLabel setTextColor:insideColor];
[iCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}else
{
[[iCell.imageView viewWithTag:999] removeFromSuperview];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}
}else
{
if (niCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
niCell = [nib objectAtIndex:0];
[niCell.contentView setBackgroundColor:[UIColor blackColor]];
[niCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
niCell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[niCell.shameLabel setTextColor:insideColor];
[niCell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[niCell.dateLabel setTextColor:insideColor];
[niCell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[niCell.userLabel setTextColor:insideColor];
[niCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}else
{
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}
}
return niCell;
}

TableView cell Labels are disappearing on scrolling, but visible for a selected cell?

As TableView cell labels are displayed on load but when the tableView is scrolled the tableview contents are not appearing. But if I select any of the cell then the labels in that particular cell are displaying.
Please help me. I have the same problem in different aspect too. I did not get any resolution for it.
Here is my code of cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"newsTableCell";
UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
[cell.contentView addSubview:user];
[user release];
UILabel *usr = (UILabel *)[cell viewWithTag:101];
usr.font = [UIFont boldSystemFontOfSize:12.0];
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
[cell.contentView addSubview:status];
[status release];
status = (UILabel *)[cell viewWithTag:102];
status.font = [UIFont boldSystemFontOfSize:12.0];
[status setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
rival.tag = 103;
[cell.contentView addSubview:rival];
[rival release];
UILabel *rivals = (UILabel *)[cell viewWithTag:103];
rivals.font = [UIFont boldSystemFontOfSize:12.0];
[rivals setText:[appDelegate.rivalusernames objectAtIndex:[indexPath row]]];
UILabel *gamename = [[UILabel alloc] initWithFrame:CGRectMake(84,27,208,21)];
gamename.tag = 104;
[cell.contentView addSubview:gamename];
[gamename release];
UILabel *gamenames = (UILabel *)[cell viewWithTag:104];
gamenames.font = [UIFont boldSystemFontOfSize:12.0];
[gamenames setText:[appDelegate.challengenames objectAtIndex:[indexPath row]]];
UILabel *time = [[UILabel alloc] initWithFrame: CGRectMake(84,47,149,21)];
time.tag = 105;
[cell.contentView addSubview:time];
[time release];
UILabel *times = (UILabel *)[cell viewWithTag:105];
times.font = [UIFont boldSystemFontOfSize:12.0];
[times setText:[appDelegate.remainingtime objectAtIndex:[indexPath row]]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
imageView.tag = 106;
[cell.contentView addSubview:imageView];
[imageView release];
NSURL *url = [NSURL URLWithString:[appDelegate.imagepaths objectAtIndex:[indexPath row]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:106];
imgView.image = img;
return cell;
}
first of all move these lines inside the if (cell==NIL):
if(cell == nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
[cell.contentView addSubview:user];
[user release];
}
or you add a subview to the cell each time you reuse it, scrolling
and the same for:
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
[cell.contentView addSubview:status];
[status release];
and
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
imageView.tag = 106;
[cell.contentView addSubview:imageView];
[imageView release];
...etc etc
as a general rule: you add/alloc new objects just once when you first create a cell (inside the if (cell==nil))
then outside that if you just reuse all objects and change their properties, as text of image source...
Maybe the color of your UILabels is the same as the background color? When you select a cell and then you can see the text, it probably is because of the then highlighted cell color ...
And I also strongly suggest you make a cell subclass as cortez mentioned, this code is not read- nor maintainable ...
your implementation of the UITableView Cell is rong try this way
i Know your problem will solve.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"newsTableCell";
UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
usr.font = [UIFont boldSystemFontOfSize:12.0];
[cell.contentView addSubview:user];
[user release];
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
status.font = [UIFont boldSystemFontOfSize:12.0];
[cell.contentView addSubview:status];
[status release];
UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
rival.tag = 103;
[cell.contentView addSubview:rival];
[rival release];
}
else
{
UILabel *usr = (UILabel *)[cell viewWithTag:101];
status = (UILabel *)[cell viewWithTag:102];
rival = (UILabel *)[cell viewWithTag:103];
}
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
[status setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
[rival setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
return cell;
}
Initially I written the code as above. After the suggestion of meronix I have changed to below one. Please suggest.
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if (tableView.tag ==16)
{
NSLog(#"%d",appDelegate.checkChallenges);
static NSString *CellIdentifier = #"newsTableCell";
UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *user = [[UILabel alloc] initWithFrame:CGRectMake(84, 10,75,21)];
user.tag = 101;
[cell.contentView addSubview:user];
[user release];
UILabel *status = [[UILabel alloc] initWithFrame:CGRectMake(154,10,69,21)];
status.tag = 102;
[cell.contentView addSubview:status];
[status release];
UILabel *rival = [[UILabel alloc] initWithFrame:CGRectMake(220,10,80,21)];
rival.tag = 103;
[cell.contentView addSubview:rival];
//[rival release];
UILabel *gamename = [[UILabel alloc] initWithFrame:CGRectMake(84,27,208,21)];
gamename.tag = 104;
[cell.contentView addSubview:gamename];
[gamename release];
UILabel *time = [[UILabel alloc] initWithFrame: CGRectMake(84,47,149,21)];
time.tag = 105;
[cell.contentView addSubview:time];
[time release];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,10,67,58)];
imageView.tag = 106;
[cell.contentView addSubview:imageView];
[imageView release];
}
UILabel *usr = (UILabel *)[cell viewWithTag:101];
usr.font = [UIFont boldSystemFontOfSize:12.0];
[usr setText:[appDelegate.usernames objectAtIndex:[indexPath row]]];
UILabel *stat = (UILabel *)[cell viewWithTag:102];
stat.font = [UIFont boldSystemFontOfSize:12.0];
[stat setText:[appDelegate.statistics objectAtIndex:[indexPath row]]];
NSURL *url = [NSURL URLWithString:[appDelegate.imagepaths objectAtIndex:[indexPath row]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:106];
imgView.image = img;
UILabel *times = (UILabel *)[cell viewWithTag:105];
times.font = [UIFont boldSystemFontOfSize:12.0];
[times setText:[appDelegate.remainingtime objectAtIndex:[indexPath row]]];
UILabel *gamenames = (UILabel *)[cell viewWithTag:104];
gamenames.font = [UIFont boldSystemFontOfSize:12.0];
[gamenames setText:[appDelegate.challengenames objectAtIndex:[indexPath row]]];
UILabel *rivals = (UILabel *)[cell viewWithTag:103];
rivals.font = [UIFont boldSystemFontOfSize:12.0];
[rivals setText:[appDelegate.rivalusernames objectAtIndex:[indexPath row]]];
return cell;
}

Error reloading custom cell after changing data

I have a set up tableView which works fine.
As you can see below I want to add an image to my cell when the value for the object #"sent" changes to #"yes". After this is done i call [sendTable reloadData] but nothing happens to the tableView. Only when I restart the app my image is properly shown in the cell.
So obviously reloadData isn't doing anything here.
Can anybody figure out the problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView == sendTable) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"savedItems.plist"];
savedItemsArray = [NSMutableArray arrayWithContentsOfFile:writableDBPath];
NSDictionary* item = [savedItemsArray objectAtIndex:indexPath.row];
NSString *temp = [NSString stringWithFormat:#"%#,%#", [[savedItemsArray valueForKey:#"name"]objectAtIndex:indexPath.row],
[[savedItemsArray valueForKey:#"message"]objectAtIndex:indexPath.row]];
UIView *background;
cell = [self.sendTable dequeueReusableCellWithIdentifier:temp];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:temp];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
background = [[UIView alloc] initWithFrame:cell.frame];
[cell addSubview:background];
[cell sendSubviewToBack:background];
}
if ([[item objectForKey:#"sent"] isEqualToString: #"no"]) {
UILabel *personLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, cell.frame.size.width-150, 20)];
personLabel.backgroundColor = [UIColor clearColor];
personLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"name"]];
personLabel.font = [UIFont boldSystemFontOfSize:17];
UILabel *personLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, cell.frame.size.width-150, 20)];
personLabel2.backgroundColor = [UIColor clearColor];
personLabel2.text = [NSString stringWithFormat: #"(%#)", [item objectForKey:#"number"]];
personLabel2.font = [UIFont systemFontOfSize:15];
UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-125, 10, 80, 40)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = [NSString stringWithFormat: #"%#\n%#",[item objectForKey:#"date"], [item objectForKey:#"time"]];
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.textAlignment = NSTextAlignmentRight;
dateLabel.font = [UIFont systemFontOfSize:13];
dateLabel.numberOfLines = 2;
UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, cell.frame.size.width-50, 20)];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"message"]];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.font = [UIFont systemFontOfSize:15];
[background addSubview:personLabel];
[background addSubview:personLabel2];
[background addSubview:dateLabel];
[background addSubview:messageLabel];
[cell addSubview:background];
}else{
UILabel *personLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 10, cell.frame.size.width-170, 20)];
personLabel.backgroundColor = [UIColor clearColor];
personLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"name"]];
personLabel.font = [UIFont boldSystemFontOfSize:17];
UILabel *personLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(40, 30, cell.frame.size.width-170, 20)];
personLabel2.backgroundColor = [UIColor clearColor];
personLabel2.text = [NSString stringWithFormat: #"(%#)", [item objectForKey:#"number"]];
personLabel2.font = [UIFont systemFontOfSize:15];
UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-125, 10, 80, 40)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = [NSString stringWithFormat: #"%#\n%#",[item objectForKey:#"date"], [item objectForKey:#"time"]];
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.textAlignment = NSTextAlignmentRight;
dateLabel.font = [UIFont systemFontOfSize:13];
dateLabel.numberOfLines = 2;
UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 60, cell.frame.size.width-80, 20)];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"message"]];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.font = [UIFont systemFontOfSize:15];
UIImage *checkmarkImage = [UIImage imageNamed:#"done.png"];
UIImageView *checkmark = [[UIImageView alloc] initWithImage:checkmarkImage];
[checkmark setFrame:CGRectMake(10, 35, 20, 20)];
[background addSubview:personLabel];
[background addSubview:personLabel2];
[background addSubview:dateLabel];
[background addSubview:messageLabel];
[background addSubview:checkmark];
[cell addSubview:background];
}
}
return cell;
}
Thanks a lot!
As you are adding several UILabel and UIView to a cell,i think u should customize your own UITableViewCell.
Try to create a class subclassing UITableviewCell, and add subview in the implement.
Hope my answer is helpful

iPhone Problem with custom cell

I try this code but it's a mistake because i have a repetition of title and description...
Someone can help me please?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[CustomCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else{
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
NSString *mediaUrl = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]mediaUrl];
NSString *noimage=#"http://www.notizie-informatiche.com/wp-content/themes/arthemia/images/imagelogo.png";
if([mediaUrl isEqualToString:noimage] == FALSE){
//DISEGNO IL FRAME PER L'IMMAGINE
CGRect frameimmagine;
frameimmagine.size.width=100; frameimmagine.size.height=120;
frameimmagine.origin.x=0; frameimmagine.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frameimmagine] autorelease];
UIImage *myImage = [UIImage imageNamed:#"uknown.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
[cell setAccessoryView:imageView];
//SCARICO L'IMMAGINE
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
asyncImage.tag = 999;
NSURL *url = [NSURL URLWithString: mediaUrl];
[asyncImage loadImageFromURL:url];
[cell.contentView addSubview:asyncImage];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//OTTENGO IL TITOLO
//cell.lbltitolo = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
NSString *titolo = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
//OTTENFO LA DESCRIZIONE
//cell.lblsottotitolo.text = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]description];
NSString *descrizione = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]description];
UILabel *cellatitoloconimmagine=[[UILabel alloc]init];
[cellatitoloconimmagine removeFromSuperview];
UILabel *cellatitolo=[[UILabel alloc]init];
[cellatitolo removeFromSuperview];
UILabel *celladescrizione=[[UILabel alloc]init];
[celladescrizione removeFromSuperview];
UILabel *celladescrizioneconimmagine=[[UILabel alloc]init];
[celladescrizioneconimmagine removeFromSuperview];
/*
[celladescrizioneconimmagine removeFromSuperview];
[celladescrizione removeFromSuperview];
*/
if([mediaUrl isEqualToString:noimage] == FALSE){
CGRect frametitoloconimmagine = CGRectMake(105.0f, 5.0f, 210, 40);
cellatitoloconimmagine.frame=frametitoloconimmagine;
cellatitoloconimmagine.text=titolo;
cellatitoloconimmagine.textAlignment = UITextAlignmentLeft;
cellatitoloconimmagine.font = [UIFont boldSystemFontOfSize:16];
cellatitoloconimmagine.numberOfLines = 2;
[cell.contentView addSubview:cellatitoloconimmagine];
CGRect framedescrizioneconimmagine = CGRectMake(105.0f, 55.0f, 210, 50);
celladescrizioneconimmagine.frame=framedescrizioneconimmagine;
celladescrizioneconimmagine.text=descrizione;
celladescrizioneconimmagine.textAlignment = UITextAlignmentLeft;
celladescrizioneconimmagine.font = [UIFont systemFontOfSize:14];
celladescrizioneconimmagine.numberOfLines = 3;
[cell.contentView addSubview:celladescrizioneconimmagine];
}
else {
CGRect frametitolo = CGRectMake(5.0f, 5.0f, 310, 40);
cellatitolo.frame=frametitolo;
cellatitolo.text=titolo;
cellatitolo.textAlignment = UITextAlignmentLeft;
cellatitolo.font = [UIFont boldSystemFontOfSize:16];
cellatitolo.numberOfLines = 2;
[cell.contentView addSubview:cellatitolo];
CGRect framedescrizione = CGRectMake(5.0f, 55.0f, 310, 50);
celladescrizione.frame=framedescrizione;
celladescrizione.textAlignment = UITextAlignmentLeft;
celladescrizione.font = [UIFont systemFontOfSize:14];
celladescrizione.numberOfLines = 3;
celladescrizione.text=descrizione;
[cell.contentView addSubview:celladescrizione];
}
return cell;
}
the cell identifier must be unique for unique cells. It seems you have repeated cells because they are all dequed with name #"cell". Also remove the "static" word.