Ios 填充分组的UITableView
我正在尝试使用NSMutableArray填充分组的UITableView。我希望数组中的每个元素都有自己的节。i、 e:一个元素,每个部分一行 这是我迄今为止编写的代码Ios 填充分组的UITableView,ios,objective-c,ios6,uitableview,Ios,Objective C,Ios6,Uitableview,我正在尝试使用NSMutableArray填充分组的UITableView。我希望数组中的每个元素都有自己的节。i、 e:一个元素,每个部分一行 这是我迄今为止编写的代码 #import "MailViewController.h" @interface MailViewController () @end @implementation MailViewController NSMutableArray *mailboxes; - (id)initWithStyle:(UITable
#import "MailViewController.h"
@interface MailViewController ()
@end
@implementation MailViewController
NSMutableArray *mailboxes;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mailboxes = [[NSMutableArray alloc] initWithObjects:@"Inbox", @"Drafts", @"Sent Items", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return mailboxes.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
return cell;
}
@end
目前情况就是这样
我怎样才能用上面描述的方法得到这个呢?第一部分:收件箱,第二部分:草稿,第三部分:已发送邮件等。我已经很接近了,但还不太清楚
谢谢。您应该更改:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
到
这些行应该放在CellForRowatineXpath:方法中
数组索引应该基于节而不是行。行始终固定为零 您应该更改:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
到
这些行应该放在CellForRowatineXpath:方法中
数组索引应该基于节而不是行。行始终固定为零 更换
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
与
替换
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.row];
与
如果还需要节标题,请实现-NSString*tableView:UITableView*tableView标题forheaderinsection:NSIntegersection并基于节返回字符串 使用其他答案中的代码,您将能够在每个部分中显示一个单元格。我更喜欢做的是有一个多维的预构建单元数组,当我需要它们时,它们就在索引[部分][行]中
您当前有一个仅包含部分的数组,并尝试按照其他答案的建议以行的形式访问它们。如果一个表组中有多个邮箱,则代码将无法工作。如果还需要节标题,请实现-NSString*tableView:UITableView*tableView titleForHeaderInSection:NSIntegersection并基于节返回字符串
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mailboxes count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
switch(indexpath.section){
case 0:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 1:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 2:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
default:
break;
}
return cell;
}
使用其他答案中的代码,您将能够在每个部分中显示一个单元格。我更喜欢做的是有一个多维的预构建单元数组,当我需要它们时,它们就在索引[部分][行]中
您当前有一个仅包含部分的数组,并尝试按照其他答案的建议以行的形式访问它们。如果一个表组中有多个邮箱,代码将无法工作。上面的屏幕截图显示,您想要行而不是节,您是在谈论节吗?因为章节有自己的委托方法来定义标题。上面的截图描述了,你想要行而不是章节,你是在谈论章节吗?因为章节有自己的委托方法来定义标题。这里不需要使用switch语句。这里不需要使用switch语句。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mailboxes count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
switch(indexpath.section){
case 0:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 1:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
case 2:
cell.textLabel.text = [mailboxes objectAtIndex:indexPath.section];
default:
break;
}
return cell;
}