Chandler Buck
Chandler Buck

Reputation: 213

detailTextLabel text not displaying for cell with UITableViewCellStyleValue1

I want to show the detail label text on the right side of the cell but I have had no success.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *CellIdentifier =@"Cell";
    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.textLabel.text = [(AGSGraphic *)[_radios objectAtIndex:indexPath.row] attributeForKey:@"radio];

    NSNumber *timestamp = [[_radios objectAtIndex:indexPath.row] attributeForKey:@"date"];
    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue] / 1000];
    NSString *date =[NSString stringWithFormat:@"%@", newDate];

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [NSDate date];
    NSDate *endDate = [f dateFromString:date];
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar
                                    components:NSMinuteCalendarUnit
                                    fromDate:endDate
                                    toDate:startDate
                                    options:0];
    NSDateComponents *components2 = [gregorianCalendar components:NSHourCalendarUnit fromDate:endDate toDate:startDate options:0];
    //NSDateComponents *components3 = [gregorianCalendar components:NSDayCalendarUnit fromDate:endDate toDate:startDate options:0];
    NSInteger mins = [components minute];
    NSInteger hours = [components2 hour];
    //NSInteger days = [components3 day];
    cell.detailTextLabel.text = @"test";
    cell.detailTextLabel.frame.origin.y, 44.0, 44.0);
    return cell;
}

When I use:

initwithstyle:UITableViewCellStyleSubtitle

It works but I don't want the detail label under the title, I want it to the right of the title. I tried changing the style to UITableViewCellStyleValue1 and it does nothing.

Upvotes: 1

Views: 1517

Answers (4)

Kusal Shrestha
Kusal Shrestha

Reputation: 1673

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = @"Left Text";
    cell.detailTextLabel.text = @"Right Text";
    // Configure the cell...

    return cell;
}

Upvotes: 0

Fantini
Fantini

Reputation: 2077

If you use

UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Your cell will be already initialized, dequeue takes the configuration of your storyboards (this is why changing the style from the init isn't working for you), to change it use the storyboards right tools and select Left Detail or Right Detail under Table View Cell's Style depending on how do you want to show your data

enter image description here

And delete this line:

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

And this:

cell.detailTextLabel.frame.origin.y, 44.0, 44.0);

Upvotes: 1

keithbhunter
keithbhunter

Reputation: 12324

In the table view in your storyboard, set the cell to Right Detail and set the reuse identifier to something. Then the beginning part of your cellForRow will look like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"TheReuseIdentifierYouSet"];
    // No more need to alloc/init a table view cell
    // ...

If you are not using a storyboard, then create a new UITableViewCell subclass and .xib. Drag a UITableViewCell out, set it to right detail and set the reuse identifier. Then you can register this class with the table view in viewDidLoad using registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier:.

Upvotes: 0

Gavin Hope
Gavin Hope

Reputation: 2193

Have you tried checking cell before the init, e.g.

UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

Upvotes: 0

Related Questions