如何使用objective-c用两个NSMutableArray填充可扩展的TableView

     2023-03-24     206

关键词:

【中文标题】如何使用objective-c用两个NSMutableArray填充可扩展的TableView【英文标题】:How to populate, Expandable TableView with two NSMutableArray using objective-c 【发布时间】:2015-06-22 10:27:52 【问题描述】:

我正在使用由 Tom Fewster 创建的 Expandable UITableview。我想使用两个NSMutableArrays 调整示例,这是一个场景,如果有人想要从 web 服务 json 数据填充可扩展/折叠树视图表,则希望实现。因此,由于在他的示例中 GroupCell 没有数组,我想知道我该怎么做?请记住,我的 Objective-C 仍然生锈,所以我要问这个问题。

我的尝试只显示组的第一个 ObjectAtIndex:indexPath:0。

我希望能够填充表格并获得这样的输出;

A组 第 1a 行 第 2a 行 第 3a 行 B 组 第 1b 行 第 2b 行 C组 第 1c 行 第 2c 行 第 3c 行 等等。

如果您以这种方式更好地理解它,您也可以使用 JSON 数据来解释您的答案。

在这里我想用 JSON 数据填充表格,以便 GroupCell 显示 class_name 和 rowCell 显示 subject_name。这是我从 JSON Web 服务解析的控制台;

(
    
    "class_id" = 70;
    "class_name" = Kano;
    subject =         (

            "subject_id" = 159;
            "subject_name" = "Kano Class";
        
    );
,
    
    "alarm_cnt" = 0;

    "class_id" = 71;
    "class_name" = Lagos;
    subject =         (

            "subject_id" = 160;
            "subject_name" = "Lagos Class";
        
    );
,
    
    "alarm_cnt" = 3;
    "class_id" = 73;
    "class_name" = Nasarawa;
    subject =         (

            "subject_id" = 208;
            "subject_name" = "DOMA Class";
        ,

            "subject_id" = 207;
            "subject_name" = "EGGON Class";
        ,

            "subject_id" = 206;
            "subject_name" = "KARU Class";
        ,

            "subject_id" = 209;
            "subject_name" = "LAFIA Class";
        ,

            "subject_id" = 161;
            "subject_name" = "Nasarawa State Class";
        
    );
,
    
    "alarm_cnt" = 2;
    "class_id" = 72;
    "class_name" = Rivers;
    subject =         (

            "subject_id" = 162;
            "subject_name" = "Rivers Class";
        
    );

)

我试过了,这是我的 sn-p

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

静态 NSString *CellIdentifier = @"RowCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *d=[_sitesJson objectAtIndex:0] ;
NSArray *arr=[d valueForKey:@"subject_name"];
NSDictionary *subitems = [arr objectAtIndex:0];
NSLog(@"Subitems: %@", subitems);
NSString *siteName = [NSString stringWithFormat:@"%@",subitems];
cell.textLabel.text =siteName;
//
NSLog(@"Row Cell: %@", cell.textLabel.text);
// just change the cells background color to indicate group separation
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundView.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:243.0/255.0 blue:1.0 alpha:1.0];

return cell;

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section

静态 NSString *CellIdentifier = @"GroupCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:2];
NSDictionary *d2 = [_regionsJson objectAtIndex:0];
NSArray *arr2 = [d2 objectForKey:@"class_name"];
NSString *regions = [[arr2 objectAtIndex:section]objectAtIndex:0];
textLabel.textColor  = [UIColor whiteColor];
textLabel.text = [NSString stringWithFormat: @"%@ (%d)", regions, (int)[self tableView:tableView numberOfRowsInSection:section]];
NSLog(@"Group cell label: %@", textLabel.text);

// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:@"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) 
    accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
 else 
    accessoryView.transform = CGAffineTransformMakeRotation(0);

return cell;

【问题讨论】:

【参考方案1】:

他,只需要一点点更新一个方法

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section

    static NSString *CellIdentifier = @"GroupCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSIndexPath *indexPath;
    NSString *regions = [[_dataGroup objectAtIndex:section]objectAtIndex:0];
    cell.textLabel.text = [NSString stringWithFormat: @"%@ ", regions];

    // We add a custom accessory view to indicate expanded and colapsed sections
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:@"ExpandableAccessoryView"]];
    UIView *accessoryView = cell.accessoryView;
    if ([[tableView indexesForExpandedSections] containsIndex:section]) 
        accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
     else 
        accessoryView.transform = CGAffineTransformMakeRotation(0);
    
    return cell; 

可以帮到你。

HTH,享受编码!

【讨论】:

太好了,已经解决了,但现在我想用 JSON 数据试试这个,如果我无法实现可扩展,我会与你分享 JSON。 根据代码,如果部分仅包含 1 行,(如第 4 部分),Tom Fewster 的代码将直接显示该行,而不是 Header 和子项。 只需在第 4 部分中多输入 1 个条目,如“Row 2d”,结束即可解决。 很好用。现在我正在努力使用相同的方法解析 JSON 数据。我稍后会更新我的问题。 Viral Savaj,感谢您的帮助,现在我更新了问题以使用 json 数据填充表格。目前我正在提取 JSON 数据,但没有填充到表中。【参考方案2】:

我认为您需要创建一个 TableView,其中包含一个sections 数组,并且每个sections 行都将使用相应的sections 数组填充。点击一个部分将展开它,它的所有行都将可见。

为了满足您的要求,您也可以按照以下步骤操作 -

1) 你的模态应该有一个用于部分的数组。 section 数组将包含 section 对象、section 名称和相应的行数组。

2) 像这样实现table view的数据源方法

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView


    return [section count];
 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section


    return 50; // sections height
 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    return nil;


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
    return nil;


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section


    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0 , 0, tableView.frame.size.width , 50)] autorelease];

    [view setBackgroundColor:[UIColor redColor]];
    view.layer.masksToBounds = YES;


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5 , 2 , view.frame.size.width - 10 , view.frame.size.height - 3)];
    label.text = ((SectionObject *)[section objectAtIndex:indexPath.section]).sectionName;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor WwhiteColor];
    label.clipsToBounds = YES;
    label.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:14.0f];
    label.layer.masksToBounds = YES;

    UIImageView *arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(view.frame.size.width - 30, 0, 17 , 17)];
    [arrowImage setCenter:CGPointMake(arrowImage.center.x , (view.frame.size.height/2) ) ];

    if(section == self.m_currentSelectedSection)
        [arrowImage setImage:self.m_upArrowImage];
    else
        [arrowImage setImage:self.m_downArrowImage];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    button.tag = section;
    [button addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];

    [view addSubview:label];
    [label release];

    [view addSubview:arrowImage];
    [arrowImage release];

    [view addSubview:button];
    [button release];

    view.clipsToBounds = YES;

    return view;




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    NSInteger count = 0;

    if(self.m_currentSelectedSection == section)
              count = [((SectionObject *)[section objectAtIndex:indexPath.section]).rowArray count];

    return count;


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    return 40.0;


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString * cellId = @"cellIdentifier";

    UITableViewCell *cell = nil;

    cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil)
    
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //customize cell
    return cell;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

当任何部分被点击时,以下事件将被调用

- (void) sectionTapped:(UIButton *)button

    self.m_currentSelectedSection = button.tag;
    [self performSelector:@selector(refreshView) withObject:nil afterDelay:POINT_ONE_SECOND];
    if(m_winnerSlotList->at(self.m_currentSelectedSection).m_leaderboardList.size())
        [self.m_leaderboardTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    UIView *baseView = [button superview];
    if(baseView)
    
        for(int ii = 0 ; ii < [[baseView subviews] count] ; ii++ )
        
            UIView *anyView = [[baseView subviews] objectAtIndex:ii];
            if([anyView isKindOfClass:[UIImageView class]])
                [(UIImageView *)anyView setImage:self.m_upArrowImage];
        
    

初始化self.m_currentSelectedSection = 0,这是第一次,这将显示第 0 部分的行。当任何部分被点击时,它的行将可见(相应的部分行将展开),前一个选定部分的行将被隐藏(前一个部分的行将折叠)。

如果您需要将多个部分显示为展开状态,则需要跟踪所有部分是否展开,并相应地加载显示/隐藏相应部分的单元格。

【讨论】:

嗨 Sanjay,我已经更新了问题,请注意我没有使用 UITableView 而是来自 link 的自定义 ExpandableTableView 感谢@Sanjay Mohnani 发布您的答案,但这似乎超出了我想要解决的范围。也许是针对不同的问题 @CockpitAliens,这是针对您要问的同一个问题,我还使用相同的方法创建了可扩展的表格列表。但仍然不是问题。 在您的方法中,您没有使用 expandabletableview,而是使用了 uitableview。 酷,你能邀请我一对一聊天吗?在vc之间传递数据需要帮助

Objective-C 中的内存管理

】Objective-C中的内存管理【英文标题】:Memorymanagementinobjective-c【发布时间】:2010-03-1109:59:27【问题描述】:我的一门课中有这段代码:-(void)processArrayNSMutableArray*array=[selfgetArray];...[arrayrelease];array=nil;-(NSMutableArray*)getArray//NO1:NSMutab 查看详情

如何在 Objective-C 中使用 startMonitoringForRegion 扫描多个区域

】如何在Objective-C中使用startMonitoringForRegion扫描多个区域【英文标题】:HowtoscanmultipleregionsusingstartMonitoringForRegioninObjective-C【发布时间】:2014-01-1505:56:39【问题描述】:我已经完成了两个教程,并且正在阅读基本的C。通过实践来... 查看详情

如何在 Objective-C 中使用 Swift 文件?

】如何在Objective-C中使用Swift文件?【英文标题】:HowtoworkwithSwiftfileinObjective-C?【发布时间】:2016-11-2405:38:07【问题描述】:我想使用一个用Swift编写的第三方库,但我在Objective-C环境中工作。怎么做?到目前为止,我所做的是:... 查看详情

如何打破 Objective-C 中的两个嵌套 for 循环?

】如何打破Objective-C中的两个嵌套for循环?【英文标题】:HowcanIbreakoutoftwonestedforloopsinObjective-C?【发布时间】:2010-10-2604:16:25【问题描述】:我有两个这样嵌套的for循环:for(...)for(...)我知道有一个break声明。但是我很困惑它是打... 查看详情

如何在 iOS 的 Objective-C 中使用对齐区域编程拖放

】如何在iOS的Objective-C中使用对齐区域编程拖放【英文标题】:howtoprogramdraganddropwithsnaptoareainObjective-CforIOS【发布时间】:2012-03-0305:25:25【问题描述】:我希望在Objective-C中为IOS平台实现一些东西。我想要两个GUI对象,为了简单起... 查看详情

Objective-C:如何让它为 NSCoding 保存两个文件而不是一个文件?

】Objective-C:如何让它为NSCoding保存两个文件而不是一个文件?【英文标题】:Objective-C:HowtomakeitsavetwofilesinsteadofoneforNSCoding?【发布时间】:2011-12-1323:45:05【问题描述】:我的代码在这里:-(void)loadDataFromDisk[dictrelease];NSMutableData*dat... 查看详情

Objective-C:将两个整数相除并返回一个四舍五入的整数值

】Objective-C:将两个整数相除并返回一个四舍五入的整数值【英文标题】:Objective-C:Dividetwointegersandreturnaroundedintegervalue【发布时间】:2012-10-1904:17:33【问题描述】:如何将两个NSInteger相除,例如13/4,然后将结果四舍五入到下一... 查看详情

如何在objective-c中比较两个具有很多属性的对象

】如何在objective-c中比较两个具有很多属性的对象【英文标题】:Howcomparetwoobjectswithalotofattributesinobjective-c【发布时间】:2014-04-0409:09:56【问题描述】:我想测试我的Basket类型的对象篮的反序列化,其中包含AProduct类型的对象数组... 查看详情

如何使用 XMPPFramework 设置匿名登录 - iOS Objective-C

】如何使用XMPPFramework设置匿名登录-iOSObjective-C【英文标题】:HowtosetupanonymousloginwithXMPPFramework-iOSObjective-C【发布时间】:2015-07-3018:09:48【问题描述】:我正在尝试设置一个匿名登录,这样我的用户就不必在eJabberd服务器上创建一... 查看详情

如何在 Flutter 插件的 Swift 编写的 iOS 部分中使用 Objective-C 框架

】如何在Flutter插件的Swift编写的iOS部分中使用Objective-C框架【英文标题】:HowtouseObjective-CframeworkinaSwiftwritteniOSpartofaflutterplugin【发布时间】:2019-06-2520:25:44【问题描述】:在Flutter插件中,我想在我的iOS部分中使用Objective-C框架,... 查看详情

Objective-C - 使用 Accelerate.framework 对两个矩阵进行元素加法(和除法)

】Objective-C-使用Accelerate.framework对两个矩阵进行元素加法(和除法)【英文标题】:Objective-C-Element-wiseaddition(anddivision)oftwomatriceswithAccelerate.framwork【发布时间】:2014-03-2902:08:38【问题描述】:我有两个矩阵(float*类型),所以我... 查看详情

如何使用 AFNetworking 3.x 在 ios、objective-C 中使用来自 SOAP webservice 的数据

】如何使用AFNetworking3.x在ios、objective-C中使用来自SOAPwebservice的数据【英文标题】:HowtouseAFNetworking3.xtoconsumedatafromSOAPwebserviceinios,objective-C【发布时间】:2015-12-2805:08:51【问题描述】:我正在开发一个使用来自Web服务的数据的应用... 查看详情

在 Objective-C 中组合韩语字符

】在Objective-C中组合韩语字符【英文标题】:CombiningKoreancharactersinObjective-C【发布时间】:2014-05-3022:54:58【问题描述】:我一直在为此挠头。我想把两个韩文字符组合成一个。ㅁ+ㅏ=마我将如何使用NSString进行此操作?编辑:zaph的... 查看详情

如何在c++中,调用objective-c

UsingC++WithObjective-C苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++。有了它,你就可以在Objective-C应用程序中使用已有的C++类库。Objective-C和C++混编的要点在Objective-C++中,... 查看详情

Objective-C 链接两个 Tableview

】Objective-C链接两个Tableview【英文标题】:Objective-ClinkingtwoTableviews【发布时间】:2017-04-0506:33:02【问题描述】:我想要的是,当您单击表格中的任何单元格时,您会转到另一个表格视图。我还需要为您单击的每个不同的单元格提... 查看详情

使用 Objective-C 在实时视频中检测眼睛?

】使用Objective-C在实时视频中检测眼睛?【英文标题】:DetecteyesinrealtimevideowithObjective-C?【发布时间】:2017-08-0508:10:37【问题描述】:我正在检查thisApplesamplecode,Squarecam。还有一些用Swift编写的示例。在本例中,检测到人脸时会绘... 查看详情

如何在 Objective-C 中的 UISlider 上添加标记? [关闭]

】如何在Objective-C中的UISlider上添加标记?[关闭]【英文标题】:HowcaniaddmarksonUISliderinobjective-c?[closed]【发布时间】:2016-04-1110:42:37【问题描述】:我想使用Objective-c在UISlider上添加标记关于UISilder的特定点。就像滑块上的这两个点/... 查看详情

在objective-c中如何创建post请求?

我在开发iOS网络应用,需要在Objective-C中如何创建post请求,代码能实现吗?可以的,在创建请求的时候用上下面的语句即可...NSMutableURLRequest*urlRequest=[[[NSMutableURLRequestalloc]init]autorelease];[urlRequestsetURL:postUrl];//设置请求地址[urlRequest... 查看详情