Crash when scrolling the uitableview - iphone

I have a uitableview which crashes when scrolling.
I have tried leaks and build and analyse but could not solve this.
getting the below error
*** -[CALayer retain]: message sent to deallocated instance 0xe192280
Below is the code i am running.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *font = [UIFont fontWithName:#"Arial" size:13];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
NSInteger row = [indexPath row];
cellRect2 = CGRectMake(135, 10, 5, 20 );
lblColon = [[UILabel alloc] initWithFrame: cellRect2];
lblColon.font = font;
lblColon.backgroundColor = [UIColor clearColor];
lblColon.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblColon];
lblColon.text = #":";
if (tableView == tblCompanyDetails) {
switch (indexPath.section)
{
case 0:
{
profileName = [appDelegate.arrCompanyProfile1 objectAtIndex:indexPath.row];
NSLog(#"appDelegate.arrCompanyProfile1 ------> %#", appDelegate.arrCompanyProfile1);
if(row == 0)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Company Name";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *str = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(#"str--------%#", str);
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 1)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Type of Company";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSLog(#"profileName.namelast--------%#", profileName);
//NSString *strLastName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 2)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Main Contact Person";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 3)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Address";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 4)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"City";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 5)
{
cellRect1 = CGRectMake(10, 10, 100, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"State";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 6)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Postal / Zip Code";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 7)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Country";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 8)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Primary Phone No.";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 9)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Mobile No.";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 10)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Toll Free No.";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 11)
{
cellRect1 = CGRectMake(10, 10, 100, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Websites";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 12)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Email Address";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 13)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Number of Employees";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 14)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Legal structure";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 1:
{
profileName = [appDelegate.arrCompanyProfile2 objectAtIndex:indexPath.row];
if(row == 0)
{
cellRect1 = CGRectMake(10, 10, 100, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Primary";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 1)
{
cellRect1 = CGRectMake(10, 10, 120, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Secondary";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSLog(#"profileName section2 ----> %#", profileName);
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 2)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"No of years in business";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 2:
{
profileName = [appDelegate.arrCompanyProfile3 objectAtIndex:indexPath.row];
if(row == 0)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Postal / Zip Code";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 1)
{
cellRect1 = CGRectMake(10, 10, 130, 20);
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Coverage Area (miles)";
cellRect3 = CGRectMake(145, 10, 180 ,20);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 3:
{
profileName = [appDelegate.arrCompanyProfile4 objectAtIndex:indexPath.row];
if(row == 0)
{
lblColon.hidden = YES;
cellRect3 = CGRectMake(10, 10, 280, 40);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 4:
{
profileName = [appDelegate.arrCompanyProfile5 objectAtIndex:indexPath.row];
if(row == 0)
{
lblColon.hidden = YES;
cellRect3 = CGRectMake(10, 10, 100, 20);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 5:
{
if(row == 0)
{
lblColon.hidden = YES;
cellRect3 = CGRectMake(10, 10, 100, 20);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
break;
}
}
}
}
}
return cell;
}

You are not actually reusing any cell because you are doing all part within the cell== nil code bloack, Changing the code something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *font = [UIFont fontWithName:#"Arial" size:13];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
UILabel* lblColon = [[UILabel alloc] initWithFrame:CGRectMake(135, 10, 5, 20 )];
lblColon.font = font;
lblColon.backgroundColor = [UIColor clearColor];
lblColon.textColor = [UIColor blackColor];
lblColon.text = #":";
[lblColon setTag:222];
[cell.contentView addSubview: lblColon];
[lblColon release];
}
NSInteger row = [indexPath row];
UILabel* lblToReuse = (UILabel*)[cell.contentView viewWithTag:222];
if (tableView == tblCompanyDetails) {
//usual things you were doing earlier
}
return cell;
}
might help you a bit. Check it if it still crashes after replacing the code with this one.

Where are you releasing all the objects you are allocating? It looks like you are trying to alloc the same objects over and over.
try the following approach:
UILabel *lblTitle = [[[UILabel alloc] initWithFrame: cellRect1] autorelease];

At first, mechanism of reusing cell doesn't work in your code, because you do:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// ...
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
You are trying to reuse cell with identifier "Cell"? but when you create cell you create it with reuseIdentifier "MyIdentifier". It's bad. You should create cell with the same identifier:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
And move your part for individually configuring of cell (depending on indexPath) outside if (cell == nil) { statement. Inside this statement you should only global configuring for your cells. Configure here only the same things for all cells in this tanle view. Because this cell will be reused for other cells.
When we are using reusing mechanism for cells it try to find cell with identifier (calling [tableView dequeueReusableCellWithIdentifier:CellIdentifier]). If it's not found, than you create cell prototype with the same identifier
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
}
and after this part of code place your specific code for current cell (at specified indexPath)

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];

Overlapping Table cells-content [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
recently I discovered a really annoying bug:
the content of two cells are mixed up in one cell
I have 16 cells data to display on scrolling the previeously existing cell data get overlapped with current data to be displayed
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (flag == NO) {
return 10;
}
return [ytdRate count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (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];
}
if (flag == NO) {
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
else if(flag == YES)
{
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[companyName objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[ytdRate objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [ytdRate objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [netAsset objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[oneYearValue objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[threeYearValue objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[fiveYearValue objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
Do you have any idea how such a behavior can occur?
Try this..
- (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 viewWithTag:111]removeFromSuperview];
[[cell viewWithTag:222]removeFromSuperview];
[[cell viewWithTag:333]removeFromSuperview];
[[cell viewWithTag:444]removeFromSuperview];
[[cell viewWithTag:555]removeFromSuperview];
[[cell viewWithTag:666]removeFromSuperview];
[[cell viewWithTag:777]removeFromSuperview];
if (flag == NO) {
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab1.tag = 111;
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.tag = 222;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab2.tag=333;
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab2.tag=444;
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab2.tag=555;
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab2.tag=666;
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab2.tag=777;
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
else if(flag == YES)
{
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[companyName objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab1.tag = 111;
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[ytdRate objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.tag = 222;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [ytdRate objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab3.tag = 333;
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [netAsset objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab8.tag = 444;
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[oneYearValue objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab4.tag = 555;
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[threeYearValue objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab5.tag = 666;
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[fiveYearValue objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab6.tag = 777;
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;}
tableView dequeueReusableCellWithIdentifier: means that you're requesting a UITableViewCell object of a certain type (identifier), and you'll get either a new or old one depending on whether or not a cell with your identifier has been created yet.
Your code requests a set of table cells and fills them with UILabels. When you scroll, your table view requests more table cells. At this point, since some cells of this type have already been created, you're getting the old objects - the old table cells with UILabels already in them - and then creating more UILabels within these cells. This is why they overlap.
You could stop using reusable cells, and simply create a new UITableViewCell for each table cell, however this is not recommended. This method would use significantly more memory as the number of table cells increases, which is the problem that reusable cells are intended to overcome. A more detailed explanation is available in this answer.
Instead, what you should do is simply clear all of the content from the reusable cells that you retrieve, ensuring that your cell is clear of any previously created content when you create your UILabels again. Adding the following code to your cellForRowAtIndexPath (after you've retrieved a reusable cell) method will do the trick:
for(UIView *view in [cell subviews])
[view removeFromSuperview];
- (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];}
for (UIView *v [cell.contenetView subviews]{
[v removefromSuperView];}
******* Change code *********
if (flag == NO) {
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
else if(flag == YES)
{
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[companyName objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[ytdRate objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [ytdRate objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [netAsset objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[oneYearValue objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[threeYearValue objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[fiveYearValue objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
NEW UPDATED ANSWER
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
}

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

TableViewCell to auto-resize its height according to content in xcode?

I need to set the height of tableViewCell according to content in it. I have some labels of dynamic height in tableViewCell,number of lines in the label is not constant. Sometimes if a one label's text is null then the other label may occupy its position. So what i need is tableViewCell should autoresize height according to the content?
-(void)viewDidLoad
{
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,327,[array count]*205) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
float fscrview = 150 + self.tableView.frame.size.height + 20;
testscroll.contentSize=CGSizeMake(320, fscrview);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(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]autorelease];
UILabel *name1 = [[UILabel alloc] initWithFrame:CGRectZero];
name1.tag = 111;
name1.backgroundColor = [UIColor clearColor];
[name1 setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14]];
[name1 setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:name1];
UILabel *code = [[UILabel alloc] initWithFrame:CGRectZero];
codetype.tag = 112;
codetype.backgroundColor = [UIColor clearColor];
[codetype setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[codetype setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:codetype];
line1 = [[UILabel alloc] initWithFrame:CGRectZero];
line1.tag = 113;
line1.backgroundColor = [UIColor clearColor];
[line1 setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[line1 setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:line1];
line3 = [[UILabel alloc] initWithFrame:CGRectZero];
line3.tag = 114;
line3.backgroundColor = [UIColor clearColor];
[line3 setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[line3 setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:line3];
city = [[UILabel alloc] initWithFrame:CGRectZero];
city.tag = 115;
city.backgroundColor = [UIColor clearColor];
[city setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[city setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:city];
}
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
NSMutableDictionary *d =[[NSMutableDictionary alloc]initWithDictionary: [arr2 objectAtIndex:indexPath.row]];
NSString *name2 = [d objectForKey:#"Name"];
CGSize constraint1 = CGSizeMake(175, 2000.0f);
CGSize size1 = [name2 sizeWithFont: [UIFont fontWithName:#"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
UILabel *name1 = (UILabel *)[cell viewWithTag:111];
name1.frame = CGRectMake(105,25, 175, size1.height);
[name1 setNumberOfLines:size1.height/16];
name1.text = [d objectForKey:#"Name"];
[name1 setTextColor:UIColorFromRGB(COLOR_BLUE)];
NSString *codetype2 = [d objectForKey:#"Type"];
CGSize constraint2 = CGSizeMake(175, 2000.0f);
CGSize size2 = [codetype2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint2 lineBreakMode:UILineBreakModeWordWrap];
UILabel *codetype1 = (UILabel *)[cell viewWithTag:112];
codetype1.frame = CGRectMake(105,name1.frame.size.height+25, 175, size2.height);
[codetype1 setNumberOfLines:size2.height/16];
codetype1.text=[d objectForKey:#"CodeType"];
NSString *line2 = [d objectForKey:#"Line1"];
NSString *line4 = [d objectForKey:#"Line2"];
if([line2 isEqualToString:#""])
{
if([line4 isEqualToString:#""])
{
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint4 = CGSizeMake(175, 2000.0f);
CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size4.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
else
{
NSString *line4 = [d objectForKey:#"Line2"];
CGSize constraint4 = CGSizeMake(175, 2000.0f);
CGSize size4 = [line4 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
line3 = (UILabel *)[cell viewWithTag:114];
line3.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size4.height);
[line3 setNumberOfLines:0];
line3.text = [d objectForKey:#"Line2"];
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,line3.frame.size.height+line3.frame.origin.y, 175, size5.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
}
else
{
NSString *line2 = [d objectForKey:#"Line1"];
CGSize constraint3 = CGSizeMake(175, 2000.0f);
CGSize size3 = [line2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint3 lineBreakMode:UILineBreakModeWordWrap];
line1 = (UILabel *)[cell viewWithTag:113];
line1.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size3.height);
[line1 setNumberOfLines:0];
line1.text = [d objectForKey:#"Line1"];
if([line4 isEqualToString:#""])
{
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,line1.frame.size.height+line1.frame.origin.y, 175, size5.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
else
{
NSString *line4 = [d objectForKey:#"Line2"];
CGSize constraint4 = CGSizeMake(175, 2000.0f);
CGSize size4 = [line4 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
line3 = (UILabel *)[cell viewWithTag:114];
line3.frame = CGRectMake(105,line1.frame.size.height+line1.frame.origin.y, 175, size4.height-3);
[line3 setNumberOfLines:0];
line3.text = [d objectForKey:#"Line2"];
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,line3.frame.size.height+line3.frame.origin.y+4, 175, size5.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
}
UILabel *state=[[UILabel alloc]initWithFrame:CGRectMake(105,city.frame.size.height+city.frame.origin.y+5,320,10)];
state.font=[UIFont fontWithName:#"Helvetica" size:12];
[state setTextAlignment:UITextAlignmentLeft];
[state setText:[d valueForKey:#"State"]];
state.tag=116;
[cell addSubview:state];
[state release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, tableView.sectionHeaderHeight)];
sectionHeaderView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
sectionHeaderView.userInteractionEnabled = YES;
sectionHeaderView.tag = section;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,2, tableView.bounds.size.width-10, 18)];
headerLabel.backgroundColor = [UIColor whiteColor];
[headerLabel setTextAlignment:UITextAlignmentCenter];
headerLabel.textColor = [UIColor grayColor];
headerLabel.shadowColor = [UIColor darkGrayColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.text = #"Details";
[sectionHeaderView addSubview:headerLabel];
return sectionHeaderView;
}
Here i have given it to be 205 .but when the text in the tableViewCell increases then i couldn't see the last lines of the label of last tableViewCell..
How can i make tableViewCell to auto-resize its height ?
You can specify the height of a cell in – tableView:heightForRowAtIndexPath:.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height=60; //set default row height
if(check_for_the_rows_which_need_increased_height) {
height+=50; //Add appropriate increase
}
return height;
}
You need to call [yourTableView reloadData] or [yourTableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic] for above method to be called again.
You can just check for you textHeight and then set your row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *Yourtext = [yourTextAry objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(yourLabelWidth , 20000.0f);
CGSize size = [Yourtext sizeWithFont: [UIFont fontWithName:#"Verdana" size:13] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height;
}
and if text is null then remove it from array dear or just pass height to zero..:)

how to place star rating image next to the textlabel

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;