简体   繁体   English

TableView部分似乎不起作用目标-C

[英]TableView sections don't seem to work Objective - C

The problem I am encountering is that I don't know how to code my tableViewController properly, so it displays 2 sections, one with Items (object type) which value is higher than 50, and second section for Items valued less than 50. My code looks like this: 我遇到的问题是我不知道如何正确编码tableViewController,因此它显示2个部分,其中一个部分的Items(对象类型)的值大于50,第二部分的Items的值小于50。代码看起来像这样:

@implementation ItemsViewController

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStylePlain];

    if(self)
    {
        for (int i = 0; i < 5; i++)
        {
            [[ItemStore sharedStore] createItem];
        }
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStylePlain];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
{
    return @[@">50", @"<=50"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return [[[ItemStore sharedStore] itemsValuedOver50] count];
    }
    else
    {
        return [[[ItemStore sharedStore] itemsValuedBelowOrEqual50] count];
    }
}

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

    if (indexPath.section == 0)
    {
        NSArray *items = [[ItemStore sharedStore] itemsValuedOver50];

        NSArray *item = items[indexPath.row];

        cell.textLabel.text = [item description];

    }
    else
    {
        NSArray *items = [[ItemStore sharedStore] itemsValuedBelowOrEqual50];

        NSArray *item = items[indexPath.row];

        cell.textLabel.text = [item description];
    }

    return cell;
}

@end

Section names (">50" and <=50) appear in the middle of the right side of the screen(looks like it is out of the table view), and the table view still behaves as it had only one section. 节名称(“> 50”和<= 50)出现在屏幕右侧的中间(看起来好像不在表格视图中),并且表格视图仍然起作用,因为它只有一个节。 These 5 items are not sorted in any way and I have no idea why. 这5个项目没有进行任何排序,我也不知道为什么。

Here is the link to screenshot of the application: http://imgur.com/3MoHmmn 以下是该应用程序屏幕截图的链接: http : //imgur.com/3MoHmmn

I've been looking for answer for some time, but to be fair i don't know how to describe my problem well enough to find any solution online, thats why I'm creating a new topic. 我一直在寻找答案,但是为了公平起见,我不知道如何很好地描述我的问题以在线找到任何解决方案,这就是为什么我要创建一个新主题。

Thanks for your help in advance. 感谢您的帮助。

EDIT: I've figured out the problem. 编辑:我已经解决了问题。 It took me few days and it was preety stupid. 我花了几天时间,这真是愚蠢。 I used wrong method. 我使用了错误的方法。 Instead of using sectionIndexTitlesForTableView: i should have used titleForHeaderInSection:. 而不是使用sectionIndexTitlesForTableView:我应该使用titleForHeaderInSection:。 Now everything works as i wanted it to work. 现在一切正常,因为我希望它能正常工作。

尝试通过代理设置截面高度

- (CGFloat)tableView:(UITableView *)tableView heightForSection:(NSUInteger)section;

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

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