我对iOS开发相当新,我一直在为这个问题的解决方案摔跤大约一天,并不知道为什么它不工作。我正在尝试在viewcontroller中使用tableview作为供用户使用的小菜单。我已经检查过NSArray是否被填充,而且是。我也检查过这个单元格是否被创建,它是。我只是不明白为什么它不会为它创建的单元格填充tableview。以下是我迄今为止的代码。预先感谢任何人都可以提供的帮助。ViewController中的TableView不显示单元格
MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *menuTableView;
@property (weak, nonatomic) IBOutlet UIButton *menuButton;
@property (strong, nonatomic) NSArray *menuItemsArray;
@property (weak, nonatomic) IBOutlet UILabel *menuLabel;
@end
MainViewController.m
#import "MainViewController.h"
@interface MainViewController()
@end
@implementation MainViewController
@synthesize menuItemsArray, menuTableView, menuButton, menuLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Set TableView Delegate/DataSource to self
[self.menuTableView setDelegate:self];
[self.menuTableView setDataSource:self];
[self.menuTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.menuTableView setBounces:NO];
[self.menuTableView setRowHeight:self.menuLabel.frame.size.height];
self.menuItemsArray = [[NSArray alloc] initWithObjects:@"Add Category", @"Add Item", @"Settings", nil];
NSLog(@"array: %@", menuItemsArray);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ([self.menuItemsArray count]);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"menuCell"];
[[cell textLabel]setText:[self.menuItemsArray objectAtIndex:indexPath.row]];
[[cell textLabel]setFont:[self.menuLabel font]];
return cell;
}
-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.menuTableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *selectedString = [self.menuItemsArray objectAtIndex:indexPath.row];
self.menuLabel.text = selectedString;
}
什么行的高度被设置为:
,如果你在你的情况下,通过创建故事板的UITableView您可以设置帧的大小? –
当前行高为44 – Luis
您可以使用“现代”的Objective-C语法吗?随着代码越来越少,这会更容易,例如:cell.textLabel.text = self.menuItemsArray [indexPath.row];检查Xcode菜单编辑 - 重构 – JOM