如何将单元格添加到 UITableView 中的自定义部分
How to add cells to custom sections in UITableView
我的 UITableView
中有三个部分,当我创建要进入此 UITableView
的对象时,我想指定我希望它进入哪个部分。我通过将它添加到三个数组之一来做到这一点。请注意,所有对象最初都添加到称为对象的容器 NSMutableArray
。
for (Profile *p in self.objects) {
if ([p.type isEqualToString:@"eol"]) {
[self.eolobjects addObject:p];
}
else if ([p.type isEqualToString:@"ae"]) {
[self.aeobjects addObject:p];
}
else if ([p.type isEqualToString:@"mw"]) {
[self.mwobjects addObject:p];
}
当我想转到 detailViewController 时出现问题
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Profile *object = self.objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
因为行:
Profile *object = self.objects[indexPath.row];
如果我单击(例如)任何部分中的第一个对象,我将始终在对象数组的第一个索引处创建该项目的对象,而不是填充数组的第一个索引中的对象我正在单击的部分。如果我将 self.objects
更改为其他三个数组中的任何一个,情况也是如此。
有没有更简单的方法可以将单元格添加到 UITableView
中的部分,或者有没有办法解决我的问题?谢谢
我的数据源方法如下所示:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section ==0) {
return @"Evolution of Life";
}
else if (section==1){
return @"Active Earth";
}
else if (section==2){
return @"Mineral Wealth";
}
return @"";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0: return self.eolobjects.count; break;
case 1: return self.aeobjects.count; break;
case 2: return self.mwobjects.count; break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (indexPath.section == 0) {
Profile *profile = self.eolobjects[indexPath.row];
cell.textLabel.text = [profile name];
return cell;
}
else if (indexPath.section ==1){
Profile *profile = self.aeobjects[indexPath.row];
cell.textLabel.text = [profile name];
return cell;
}
else if (indexPath.section ==2){
Profile *profile = self.mwobjects[indexPath.row];
cell.textLabel.text = [profile name];
return cell;
}
return cell;
}
你有两个解决方案
试试这个
Profile *object =nil;
switch (indexPath.) {
case 0: object = self.eolobjects[indexPath.row]; break;
case 1: object = self.aeobjects[indexPath.row]; break;
case 2: object = self.mwobjects[indexPath.row]; break;
}
或者您可以将所有 3 个数组放在一个数组中并像这样轻松访问它们 allObjects[indexPath.section][indexPath.row]
我的 UITableView
中有三个部分,当我创建要进入此 UITableView
的对象时,我想指定我希望它进入哪个部分。我通过将它添加到三个数组之一来做到这一点。请注意,所有对象最初都添加到称为对象的容器 NSMutableArray
。
for (Profile *p in self.objects) {
if ([p.type isEqualToString:@"eol"]) {
[self.eolobjects addObject:p];
}
else if ([p.type isEqualToString:@"ae"]) {
[self.aeobjects addObject:p];
}
else if ([p.type isEqualToString:@"mw"]) {
[self.mwobjects addObject:p];
}
当我想转到 detailViewController 时出现问题
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Profile *object = self.objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
因为行:
Profile *object = self.objects[indexPath.row];
如果我单击(例如)任何部分中的第一个对象,我将始终在对象数组的第一个索引处创建该项目的对象,而不是填充数组的第一个索引中的对象我正在单击的部分。如果我将 self.objects
更改为其他三个数组中的任何一个,情况也是如此。
有没有更简单的方法可以将单元格添加到 UITableView
中的部分,或者有没有办法解决我的问题?谢谢
我的数据源方法如下所示:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section ==0) {
return @"Evolution of Life";
}
else if (section==1){
return @"Active Earth";
}
else if (section==2){
return @"Mineral Wealth";
}
return @"";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0: return self.eolobjects.count; break;
case 1: return self.aeobjects.count; break;
case 2: return self.mwobjects.count; break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (indexPath.section == 0) {
Profile *profile = self.eolobjects[indexPath.row];
cell.textLabel.text = [profile name];
return cell;
}
else if (indexPath.section ==1){
Profile *profile = self.aeobjects[indexPath.row];
cell.textLabel.text = [profile name];
return cell;
}
else if (indexPath.section ==2){
Profile *profile = self.mwobjects[indexPath.row];
cell.textLabel.text = [profile name];
return cell;
}
return cell;
}
你有两个解决方案
试试这个
Profile *object =nil; switch (indexPath.) { case 0: object = self.eolobjects[indexPath.row]; break; case 1: object = self.aeobjects[indexPath.row]; break; case 2: object = self.mwobjects[indexPath.row]; break; }
或者您可以将所有 3 个数组放在一个数组中并像这样轻松访问它们
allObjects[indexPath.section][indexPath.row]