如何正确设置tableview的行数Xcode?
How to correctly set the number of rows of a tableview Xcode?
我试图使用以下方法在我的控制器中设置我的 table 视图的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.offers.count;
}
offers是我在viewDidLoad方法中声明并填写的NSArray:
- (void)viewDidLoad {
[super viewDidLoad];
//Fill arrays accordingly
if ([self.item isEqualToString:@"Laptops"]){
self.offers = @[@"MacBook Air", @"MacBook Pro", @"HP"];
self.prices = @[@800, @1200, @600];
self.offerNames = [[NSMutableArray alloc] init];
self.offerPrices = [[NSMutableArray alloc] init];
self.offerNames = [NSMutableArray arrayWithArray:_offers];
self.offerPrices = [NSMutableArray arrayWithArray:_prices];
}
}
当我尝试 运行 应用程序时,出现以下错误:
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xe4a88af02df0791a
我不知道我的代码到底出了什么问题,而且我以前从未遇到过这个错误。
P.S:我使用相同的方法在其他控制器中设置其他 table 视图,但 none 出现此错误。
这是头文件:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface OffersTableViewController : UITableViewController
@property NSString *item;
@property (strong, nonatomic) NSMutableArray *offerNames;
@property (strong, nonatomic) NSMutableArray *offerPrices;
@end
NS_ASSUME_NONNULL_END
那是我设置项目值的地方:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
OffersTableViewController *offers = segue.destinationViewController;
NSString *chosenItem = [[NSString alloc]init];
if (isFiltered){
chosenItem = filteredArray[indexPath.row];
}
else{
chosenItem = self.itemNames[indexPath.row];
}
offers.item = chosenItem;
}
感谢帮助
替换
if ([self.item isEqualToString:@"Laptops"])
和
if ([[NSString stringWithFormat:@"%@", item] isEqualToString:@"Laptops"]])
在进行一些调试并注释掉一些代码行并缩小我的搜索范围之后。我意识到我需要修复这行代码
cell.detailText.text = self.prices[indexPath.row];
收件人:
NSString *detailText = [NSString stringWithFormat: @"$ %@", self.prices[indexPath.row];
cell.detailText.text = detailText;
我试图使用以下方法在我的控制器中设置我的 table 视图的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.offers.count;
}
offers是我在viewDidLoad方法中声明并填写的NSArray:
- (void)viewDidLoad {
[super viewDidLoad];
//Fill arrays accordingly
if ([self.item isEqualToString:@"Laptops"]){
self.offers = @[@"MacBook Air", @"MacBook Pro", @"HP"];
self.prices = @[@800, @1200, @600];
self.offerNames = [[NSMutableArray alloc] init];
self.offerPrices = [[NSMutableArray alloc] init];
self.offerNames = [NSMutableArray arrayWithArray:_offers];
self.offerPrices = [NSMutableArray arrayWithArray:_prices];
}
}
当我尝试 运行 应用程序时,出现以下错误:
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xe4a88af02df0791a
我不知道我的代码到底出了什么问题,而且我以前从未遇到过这个错误。 P.S:我使用相同的方法在其他控制器中设置其他 table 视图,但 none 出现此错误。
这是头文件:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface OffersTableViewController : UITableViewController
@property NSString *item;
@property (strong, nonatomic) NSMutableArray *offerNames;
@property (strong, nonatomic) NSMutableArray *offerPrices;
@end
NS_ASSUME_NONNULL_END
那是我设置项目值的地方:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
OffersTableViewController *offers = segue.destinationViewController;
NSString *chosenItem = [[NSString alloc]init];
if (isFiltered){
chosenItem = filteredArray[indexPath.row];
}
else{
chosenItem = self.itemNames[indexPath.row];
}
offers.item = chosenItem;
}
感谢帮助
替换
if ([self.item isEqualToString:@"Laptops"])
和
if ([[NSString stringWithFormat:@"%@", item] isEqualToString:@"Laptops"]])
在进行一些调试并注释掉一些代码行并缩小我的搜索范围之后。我意识到我需要修复这行代码
cell.detailText.text = self.prices[indexPath.row];
收件人:
NSString *detailText = [NSString stringWithFormat: @"$ %@", self.prices[indexPath.row];
cell.detailText.text = detailText;