Ghobs
Ghobs

Reputation: 859

UITableViewCell Subtitle not showing up

I have my UITableView displaying JSON info being returned from a cloud code function. For some reason, it correctly displays the titles of the items being returned, but I can't get it to display the price as a subtitle of each cell. Setting the style as UITableViewCellStyleSubtitle doesn't seem to work, and gives me a warning stating Implicit conversion from enumeration type 'enum UITableviewCellStyle' to different enumeration type.

MatchCenterViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"

@interface MatchCenterViewController : UIViewController <UITableViewDataSource>

@property (nonatomic) IBOutlet NSString *itemSearch;

@property (nonatomic, strong) NSArray *imageURLs;
@property (strong, nonatomic) NSString *matchingCategoryCondition;
@property (strong, nonatomic) NSString *matchingCategoryLocation;
@property (strong, nonatomic) NSNumber *matchingCategoryMaxPrice;
@property (strong, nonatomic) NSNumber *matchingCategoryMinPrice;


@property (strong, nonatomic) NSArray *matchCenterArray;


@end

MatchCenterViewController.m:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end

@implementation MatchCenterViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//        _matchCenter.dataSource = self;
//        _matchCenter.delegate = self;
//        [self.view addSubview:self.matchCenter];
    }
    return self;



}




- (void)viewDidLoad
{

    [super viewDidLoad];



    self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
    self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-200);
    _matchCenter.dataSource = self;
    _matchCenter.delegate = self;
    [self.view addSubview:self.matchCenter];

    self.matchCenterArray = [[NSArray alloc] init];



}

- (void)viewDidAppear:(BOOL)animated
{

    self.matchCenterArray = [[NSArray alloc] init];

    [PFCloud callFunctionInBackground:@"MatchCenterTest"
                       withParameters:@{
                                        @"test": @"Hi",
                                        }
                                block:^(NSDictionary *result, NSError *error) {

                                    if (!error) {
                                         self.matchCenterArray = [result objectForKey:@"Top 3"];


                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            [_matchCenter reloadData];
                                        });


                                        NSLog(@"Test Result: '%@'", result);
                                    }
                                }];

    [self.matchCenter registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];


}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return [self.matchCenterArray count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    NSDictionary *matchCenterDictionary= [self.matchCenterArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [matchCenterDictionary objectForKey:@"Title"];// title of the item

    cell.detailTextLabel.text = [matchCenterDictionary objectForKey:@"Price"];// price of the item

    return cell;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Upvotes: 2

Views: 1714

Answers (2)

user19634
user19634

Reputation: 15

In Table View subtitle is not present otherwise you customize your cell use detail text label cell.detailTextLabel.text = [array objectForKey:@"Value"];

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90117

The cell you have registered is a default styled UITableViewCell, it does not have a subTitle. You can't change the style of the cell once it's created.

You basically have two options:

create a simple UITableViewCell subclass that uses UITableViewCellStyleSubtitle (or any other style of your choice)

@interface MyTableViewCell : UITableViewCell
@end

@implementation MyTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    // overwrite style
    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    return self;
}
@end

...

[self.matchCenter registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"Cell"];

Or return to the old style dequeue technique where you create a cell if none could be dequeued

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    /* configure */
}

If you do this, you have to remove the registerClass:forCellReuseIdentifier: call.

I would go with option one, because most likely pretty soon you will figure out that the built in cells are very limited and you want to add your own views (e.g. labels, image views). And if you use a subclass you can use properties to access those views, and don't have to use hacks like tags to access them.

Upvotes: 10

Related Questions