Welcome, Guest
Username Password: Remember me

Add required asterisk to cells
(1 viewing) (1) Guest

TOPIC: Add required asterisk to cells

Add required asterisk to cells 1 year, 2 months ago #1

Hello,

I'd like to add a required asterisk to all cell types, but I'm not quite sure of the best way to do this. If I was just customizing a specific type of cell, I could subclass it and implement it there, but since I want it to apply to all cells, it's a little trickier. Basically I want it to look like this:



Since it's a different color, I need to use a separate label, unfortunately. Any thoughts on what the best way to accomplish this is?

Thanks!
Last Edit: 1 year, 2 months ago by kyleslattery.

Re: Add required asterisk to cells 1 year, 2 months ago #2

Hi Kyle,

The easiest way I can think of is to add an asterisk UILabel to the cell in the didLayoutSubviewsForCell delegate method (STV 2.0 and later). Here is some sample code:

 
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
didLayoutSubviewsForCell:(SCTableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.valueRequired)
{
UILabel *asteriskLabel;
NSString *asteriskString = @"*";
 
// check if asteriskLabel already exists (in case cell is being reused/redisplayed)
asteriskLabel = (UILabel *)[cell.contentView viewWithTag:100];
if(!asteriskLabel)
{
asteriskLabel = [[UILabel alloc] init];
asteriskLabel.tag = 100;
asteriskLabel.textColor = [UIColor redColor];
 
// align to your own preference
CGRect asteriskLabelFrame = cell.textLabel.frame;
asteriskLabelFrame.origin.x += cell.textLabel.frame.size.width;
asteriskLabelFrame.size.width =
[asteriskString sizeWithFont:asteriskLabel.font].width;
asteriskLabel.frame = asteriskLabelFrame;
 
[cell.contentView addSubview:asteriskLabel];
[asteriskLabel release];
}
asteriskLabel.text = asteriskString;
}
}
 


Please tell me if this works well for you.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72
Last Edit: 1 year, 2 months ago by tarekskr.

Re: Add required asterisk to cells 1 year, 2 months ago #3

Hm, it does work, but only for the first cell in a section. I added a breakpoint in the method, and it's definitely getting run multiple times, but for whatever reason, it's not being applied to all of the cells:



(All of those are marked with valueRequired = YES)

Thanks for your help so far!

Re: Add required asterisk to cells 1 year, 2 months ago #4

Also, if I remove valueRequired from the first cell of the section, it still doesn't show on the following items.

Re: Add required asterisk to cells 1 year, 2 months ago #5

Sorry to keep adding more posts, but I noticed this, all the cells after the first on in the section have a frame of 0,0,0,0:

<UILabel: 0x4c53920; frame = (0 0; 0 0); text = 'Marque'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x4c53890>>

Re: Add required asterisk to cells 1 year, 2 months ago #6

Hi Kyle,

It turns out that accessing cell.textLabel.frame in the willDisplayCell method is not always reliable, as apparently this value is sometimes reset to {0,0,0,0} after the cell is displayed (one of the mysteries of UITableView! ). The correct way (which happens to be more efficient too) is to implement the above code in the didLayoutSubviewsForCell method instead, which is only available starting STV 2.0. I've updated the above code to reflect that.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72
Time to create page: 1.87 seconds