简体   繁体   English

表格视图动态中的iOS节数

[英]IOS Number Of Sections In Table View Dynamic

I am creating an app where users can store an object in Core Data. 我正在创建一个应用程序,用户可以在其中将对象存储在Core Data中。 I have the Objects being pulled into a UITableView and everything is working correctly there. 我有对象被拉入UITableView,那里一切正常。 I now want to separate the objects into a possible of 1-3 different sections based on choices in a UISegmentedControl. 我现在想根据UISegmentedControl中的选择将对象分成1-3个不同的部分。

Currently I have this to create the 1 section and populate the cells of that section with my objects 目前,我已经创建了1部分,并用我的对象填充了该部分的单元格

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.fetchedDiscs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DiscCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Configure Cell
    Disc *currentDisc = [self.fetchedDiscs objectAtIndex:indexPath.row];

    cell.textLabel.text = currentDisc.name;
    cell.detailTextLabel.text = currentDisc.brand;

    return cell;
}

So the main question is how to dynamically change the number of sections and number of rows in section? 因此,主要问题是如何动态更改节数和节中的行数?

The Segmented control returns values such as Option 1, Option 2, Option 3. The only way I can think of is to loop through my fetchedDiscs array and separate that into an array for each section. 分段控件返回诸如选项1,选项2,选项3之类的值。我能想到的唯一方法是循环遍历fetchedDiscs数组并将其分成每个部分的数组。 Then I can return the number of arrays if they exists and I can get the count of each array to get the number of rows in each section. 然后,我可以返回数组的数目(如果存在),并且我可以获取每个数组的计数以获取每个节中的行数。 But then I get to the problem of How to get the CellForRowAtIndextPath to work correctly with three arrays. 但是然后我遇到了一个问题,即如何使CellForRowAtIndextPath与三个数组一起正常工作。

Basically there has to be a better more logical way to do this. 基本上,必须有一种更好的,更合乎逻辑的方法来执行此操作。 I am just not sure how. 我只是不确定如何。

Let, you have a dictionary named dataSource that contains 'x' number of array as value for key "key1", "key2", .. "keyX". 假设您有一个名为dataSource的字典,其中包含数组的“ x”个数字作为键“ key1”,“ key2”,..“ keyX”的值。 You can do this: 你可以这样做:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[dataSource allKeys] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //Get the array object form key. I am considering 'keySection' as an example
    return [[dataSource objectForKey:@"keySection"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DiscCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [[dataSource objectForKey:keySection] objectAtIndex:indexPath.row]

    return cell;
}

Note: I wrote the code in this editor, excuse any mistakes, I just tried to share the idea. 注意:我在此编辑器中编写了代码,对不起任何错误,我只是想分享这个想法。 Hope this helps.. :) 希望这可以帮助.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM