Ios UITableViewCell字幕未显示
我的UITableView显示了从云代码函数返回的JSON信息。出于某种原因,它正确地显示了返回的项目的标题,但我无法让它将价格显示为每个单元格的副标题。将样式设置为Ios UITableViewCell字幕未显示,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的UITableView显示了从云代码函数返回的JSON信息。出于某种原因,它正确地显示了返回的项目的标题,但我无法让它将价格显示为每个单元格的副标题。将样式设置为UITableViewCellStyleSubtitle似乎不起作用,并向我发出警告,说明从枚举类型“enum UITableviewCellStyle”隐式转换为不同的枚举类型 MatchCenterViewController.h: #import <UIKit/UIKit.h> #import <Parse/
UITableViewCellStyleSubtitle
似乎不起作用,并向我发出警告,说明从枚举类型“enum UITableviewCellStyle”隐式转换为不同的枚举类型
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
#导入
#进口
#导入“AsyncImageView.h”
#导入“SearchViewController.h”
@接口匹配CenterViewController:UIViewController
@属性(非原子)IBMString*itemSearch;
@属性(非原子,强)NSArray*imageURL;
@属性(强,非原子)NSString*匹配类别条件;
@属性(强,非原子)NSString*匹配类别位置;
@属性(强,非原子)NSNumber*匹配类别映射;
@属性(强,非原子)NSNumber*匹配类别价格;
@属性(强,非原子)NSArray*matchCenterArray;
@结束
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
#导入“MatchCenterViewController.h”
#进口
@接口匹配CenterViewController()
@属性(非原子,强)UITableView*matchCenter;
@结束
@CenterViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//self.matchCenter=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//_matchCenter.dataSource=self;
//_matchCenter.delegate=self;
//[self.view addSubview:self.matchCenter];
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
self.matchCenter=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame=CGRectMake(0,50320,self.view.frame.size.height-200);
_matchCenter.dataSource=self;
_matchCenter.delegate=self;
[self.view addSubview:self.matchCenter];
self.matchCenterArray=[[NSArray alloc]init];
}
-(无效)视图显示:(BOOL)动画
{
self.matchCenterArray=[[NSArray alloc]init];
[PFCloud callFunctionInBackground:@“MatchCenter测试”
带参数:@{
@“测试”:@“嗨”,
}
块:^(NSDictionary*结果,NSError*错误){
如果(!错误){
self.matchCenterArray=[result objectForKey:@“前3名”];
dispatch\u async(dispatch\u get\u main\u queue()^{
[_matchCenter重新加载数据];
});
NSLog(@“测试结果:'%@'”,结果);
}
}];
[self.matchCenter注册表类:[UITableViewCell类]强制重用标识符:@“Cell”];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.matchCenterArray count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary*matchCenterDictionary=[self.matchCenterArray objectAtIndex:indexath.row];
cell.textlab.text=[matchCenterDictionary objectForKey:@“Title”];//项的标题
cell.detailTextLabel.text=[matchCenterDictionary objectForKey:@“Price”];//项目的价格
返回单元;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
@结束
您注册的单元格是默认样式的UITableViewCell,它没有字幕。创建单元后,无法更改其样式
您基本上有两种选择:
创建一个简单的UITableViewCell子类,该子类使用UITableViewCellStyleSubtitle(或您选择的任何其他样式)
或者返回旧式的出列技术,如果没有可以出列的单元格,则创建一个单元格
- (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 */
}
如果执行此操作,则必须删除注册表类:forCellReuseIdentifier:调用
我会选择选项一,因为很可能很快你就会发现内置单元格非常有限,你想添加自己的视图(例如标签、图像视图)。如果您使用子类,您可以使用属性访问这些视图,而不必使用标签之类的黑客来访问它们。在表视图中不存在副标题,否则您可以自定义单元格 使用详细信息文本标签
cell.detailTextLabel.text=[array objectForKey:@“Value”] 我发现第二个选项更加灵活和可扩展。从未使用过
viewWithTag:
。我为什么要这么做?
- (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 */
}