gpt4 book ai didi

ios - UITableViewCell 副标题不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:09 25 4
gpt4 key购买 nike

我的 UITableView 显示从云代码函数返回的 JSON 信息。出于某种原因,它正确显示了退回商品的标题,但我无法将价格显示为每个单元格的副标题。将样式设置为 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

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

最佳答案

您注册的单元格是默认样式的 UITableViewCell,它没有子标题。单元格的样式一旦创建就无法更改。

你基本上有两个选择:

创建一个使用 UITableViewCellStyleSubtitle(或您选择的任何其他样式)的简单 UITableViewCell 子类

@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"];

或者返回到旧式出列技术,如果没有出列则创建一个单元格

- (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 */
}

如果这样做,您必须删除 registerClass:forCellReuseIdentifier: 调用。

我会选择选项一,因为很可能很快您就会发现内置单元格非常有限并且您想添加自己的 View (例如标签、 ImageView )。如果您使用子类,您可以使用属性来访问这些 View ,而不必使用标签之类的技巧来访问它们。

关于ios - UITableViewCell 副标题不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24131805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com