Hey all, I am trying to add custom labels to my cell in UITableView. When I try to do this the display is messed up and I am unable to figure out what exactly is going on. Please find the image below and I will post the method I wrote..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell.textLabel.font=[UIFont systemFontOfSize:16];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
NSArray *contactSection=[contactNames objectForKey:contact];
NSMutableArray *sugar=[db sugarId];
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
NSArray *splitText = [cellValue componentsSeparatedByString:@":"];
NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
//NSString *result=[contactText stringByAppendingString:[sugar objectAtIndex:[indexPath row]]];
firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];
firstLabel.tag =40; //This should be a constant probably
firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
firstLabel.text = contactText;
[firstLabel sizeToFit];
[cell.contentView addSubview:firstLabel];
sugarLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
sugarLabel.tag =40; //This should be a constant probably
//sugarLabel.font = [UIFont boldSystemFontOfSize:16];
sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
[sugarLabel setHidden:YES];
[cell.contentView addSubview:sugarLabel];
cell.textLabel.text =firstLabel.text;
} else {
NSString *cellText = [contactSection objectAtIndex:[indexPath row]];
// split the text by the : to get an array containing { "AAA", "BBB" }
NSArray *splitText = [cellText componentsSeparatedByString:@":"];
// form a new string of the form "BBB AAA" by using the individual entries in the array
NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];
firstLabel.tag =40; //This should be a constant probably
//firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
firstLabel.text = contactText;
[firstLabel sizeToFit];
[cell.contentView addSubview:firstLabel];
sugarLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
sugarLabel.tag =40; //This should be a constant probably
//sugarLabel.font = [UIFont boldSystemFontOfSize:16];
sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
[sugarLabel setHidden:YES];
[cell.contentView addSubview:sugarLabel];
NSString *result=[NSString stringWithFormat:@"%@ %@",firstLabel.text,sugarLabel.text];
cell.textLabel.text=result;
}
Figure 2: This is modification to the code as suggested by BP. The result is here under..
EDIT:
Whenever I try to write stringWithFormat or the above statement, the memory is not freed and the labels are getting overlap over one other. Is there a way to solve this problem..please help me..I spent more than half a day trying to figure this but no luck