Ios 包含3个组的uitable视图
我正在开发一个ios应用程序,其中包含一个视图控制器和一个表视图。我试图加载3个组中的项目列表,但当我编译它时,它显示了正确的计数,但没有显示所有项目JU的重复项目。请帮忙。让我把代码贴在这里Ios 包含3个组的uitable视图,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在开发一个ios应用程序,其中包含一个视图控制器和一个表视图。我试图加载3个组中的项目列表,但当我编译它时,它显示了正确的计数,但没有显示所有项目JU的重复项目。请帮忙。让我把代码贴在这里 @interface ViewController () @end @implementation ViewController { NSArray *menuItems; NSArray *menuItems2; NSArray *dash; } - (id)initWi
@interface ViewController ()
@end
@implementation ViewController {
NSArray *menuItems;
NSArray *menuItems2;
NSArray *dash;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
menuItems = @[@"itm1", @"itm2", @"itm3", @"itm4"];
menuItems2 = @[@"itm1", @"itm2", @"itm3", @"itm4"];
dash = @[@"itm1", @"itm2"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return [menuItems count];
}
if (section == 1) {
return [menuItems2 count];
}
if (section == 2) {
return [dash count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
@end
需要编写
cellforrowatinexpath…
方法,以便根据单元格的节和行使用适当的数据填充单元格。现在,除了使用空单元格外,您什么也不做。您的cellforrowatinexpath…
方法需要写入,以便根据单元格的节和行使用适当的数据填充单元格。现在,除了使用空单元格,您什么都不做。您不配置单元格。退出单元格队列后,必须设置其标题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Get the correct array, depending on the current section.
NSArray *items = nil;
if (indexPath.section == 0) {
items = menuItems;
}
else if (indexPath.section == 1) {
items = menuItems2;
}
else if (indexPath.section == 2) {
items = dash;
}
// Get the string from the array and set the cell's title
NSString *title = items[indexPath.row];
cell.textLabel.text = title;
return cell;
}
您不需要配置单元格。退出单元格队列后,必须设置其标题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Get the correct array, depending on the current section.
NSArray *items = nil;
if (indexPath.section == 0) {
items = menuItems;
}
else if (indexPath.section == 1) {
items = menuItems2;
}
else if (indexPath.section == 2) {
items = dash;
}
// Get the string from the array and set the cell's title
NSString *title = items[indexPath.row];
cell.textLabel.text = title;
return cell;
}
使用
if/else
或switch
语句会更好。True。我刚刚复制了OP对numberOfRowsInSection的实现,以获得一致的风格。鼓手B我尝试了这个方法,但没有出现相同的结果:(使用if/else
或switch
语句会更好。正确。我刚刚复制了OP对numberOfRowsInSection的实现,以获得一致的样式。鼓手B我尝试了这一点,但没有出现相同的结果:(@ElGuapo OP甚至不使用行。实际上…“forIndexPath”是吗now@ElGuapo但这只是为索引路径提供了一个空的原型单元格。OP的代码根本没有试图用任何数据填充单元格。@ElGuapo OP甚至不使用该行。实际上…“forIndexPath”是吗now@ElGuapo但这只是为索引路径提供了一个空的原型单元格。OP的代码根本不尝试用任何数据填充单元格。您发布的代码不会编译。在numberofrowsinssection:
方法的末尾没有return
。您发布的代码不会编译。没有return
在numberofrowsin节:
方法的末尾。