numberOfRowsInSection从最后一节开始 [英] numberOfRowsInSection starts with last section

查看:61
本文介绍了numberOfRowsInSection从最后一节开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我有一个非常简单的问题(这是肯定的)我找不到根本原因。

actually I'm have a very simple problem (that's for sure) I just can't find the root cause.

问题是,在调用之后numberOfSectionsInTableView方法获取Sections的数量(在我的情况下是24)方法numberOfRowsInSection被调用但是最后一节(23)。

The problem is, that after calling the numberOfSectionsInTableView Method to get the number of Sections (24 in my case) the method numberOfRowsInSection gets called but with the last section (23).

这导致一个越界索引滚动时。

This causes an out of bound index when scrolling.

那么,会发生什么?
我收到了最后一节(23)的数据,甚至是最后一节(23)的行数,之后这些节正在向前移动,如0,1,2,3等...

So, what happens? I'm receiving the data of the last section (23) and even the number of rows of the last section (23) and afterwards the sections are moving forward like 0,1,2,3,etc...

一个例子:
我有一个部分标题为#的特殊字符。在我的情况下,最后一部分是以X开头的所有值的X。

An example: I have a section with the section header "#" for special characters. In my case the last section is the "X" for all values beginning with an "X".

当我调用我的应用程序时,部分标题已正确设置为#但数据显示所有X值。
此时我有31个X值。当我现在滚动到31行中的最后一行时,应用程序因索引错误而崩溃。

When I call my App, the section title has been set correctly to "#" but the data shows all the "X" values. At this point I have 31 "X" values. When I now scroll to the last of the 31 rows, the app crashes because of an out of index error.

我认为一切都能正常工作如果这些部分不会从最后一个开始,然后继续第一个(23,0,1,2,3,4等)。
我找不到第一个地方有哪个地方或者为什么这个地方有价值23的地方。

I think everything would work correctly If the sections wouldn't start with the last one and afterwards continue with the first ones (23,0,1,2,3,4,etc). I just can't find the place where or why the section hast the value 23 on the first place.

---编辑2开始:
初始化数组:

---EDIT 2 BEGINS: initialising the array:

    alphabetArray = [[NSMutableArray alloc] init];

首先我填写这样的字典(包括所有字母,包括特殊字符):

First I fill a dictionary like this (for all letters incl. special chars):

charDict = [[NSMutableDictionary alloc] init];

if ([charSpecialArray count] > 0)   {

    [charDict setObject:charSpecialArray forKey:@"Special"];
    [alphabetArray addObject:@"Special"];
}

if ([charAArray count] > 0) {

    [charDict setObject:charAArray forKey:@"A"];
    [alphabetArray addObject:@"A"];
}

---编辑2结束

所以这里我只是填充第一个字典对象的所有#值(继续所有其他值)

So here I'm just filling the first dictionary Object with all "#" values (going on with all the other values)

然后我打电话给numberOfSectionsInTableView方法:

Then I call the numberOfSectionsInTableView method:

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

此处numberOfRowsInSection为:

here the numberOfRowsInSection goes:

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSString *key = [alphabetArray objectAtIndex:section];  
NSArray *array = [charDict objectForKey:key];
return [array count];
 }

}

和cellForRowAtIndexPath:

and the cellForRowAtIndexPath:

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

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if(cell == nil)
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];

if ([charSpecialArray count] > 0)   {

    Char *charSpecial = (Char *)[[charSpecialArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell.textLabel.numberOfLines = 1;
    cell.detailTextLabel.numberOfLines = 2;
    [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];

    cell.textLabel.text = charSpecial.ta;
    cell.detailTextLabel.text = charSpecial.descriptionEN;
}

if ([charAArray count] > 0) {

    Char *charA = (Char *)[[charAArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell.textLabel.numberOfLines = 1;           
            cell.detailTextLabel.numberOfLines = 2;
    [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];

    cell.textLabel.text = charA.ta;
    cell.detailTextLabel.text = charA.descriptionEN;

    }

再次为所有字符...

again for all characters...

有任何建议如何修复?

已添加
初始化代码.h:

added initialization code .h:

#import <UIKit/UIKit.h>

@interface Char : NSObject {
NSString *ta;
NSString *report;
NSString *descriptionDE;
NSString *descriptionEN;
}

@property (nonatomic, retain) NSString *ta;
@property (nonatomic, retain) NSString *report;
@property (nonatomic, retain) NSString *descriptionDE;
@property (nonatomic, retain) NSString *descriptionEN;


-(id)initWithTa:(NSString *)t report:(NSString *)re descriptionDE:(NSString *)dde     descriptionEN:(NSString *)den;

@end

初始化代码.m:

#import "SCode.h"

@implementation Char

@synthesize ta, report, descriptionDE, descriptionEN;

-(id)initWithTa:(NSString *)t report:(NSString *)re descriptionDE:(NSString *)dde descriptionEN:(NSString *)den {
self.ta = t;
self.report = re;
self.descriptionDE = dde;
self.descriptionEN = den;
return self;
}

- (void)dealloc {

[ta release];
[report release];
[descriptionDE release];
[descriptionEN release];
[super dealloc];
}

@end


推荐答案

您的 cellForRowAtIndexPath 存在严重问题。无论您在哪个部分(只要它有一个项目),都可以访问CharsSpecial,并且它可能没有该部分那么多的项目。这很可能导致崩溃。如果您的代码编写得很好,那么乱序表不应该崩溃。

You've got a big problem with your cellForRowAtIndexPath. CharsSpecial is accessed no matter what section you are in (so long as it has one item), and it may very well not have as many items as that section. This is most likely causing the crash. If your code is well written, then an out of order table shouldn't crash anything.

IndexPath有两个组件,section和row。为了从2D数组中获取所需数据,您应该使用 [[topArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] 。同样,numberOfRows应使用 [[topArray objectAtIndex:indexPath.section] count] ,numberOfSections应使用 [topArray count] 。这样,所有这些方法都是一致的。你需要对字典进行调整,但这应该是微不足道的。

IndexPath has two components, section and row. In order to get the data you want out of a 2D array, you should use [[topArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]. Likewise, numberOfRows should use [[topArray objectAtIndex:indexPath.section] count] and numberOfSections should use [topArray count]. This way, all of these methods are consistent. You will need to adapt this for dictionaries, but that should be trivial.

完成后你可以滚动表格,发布关于订单的新问题您的数据,并包括修改listOfItems的所有代码。

Once you've done that and you can scroll the table, post a new question about the order of your data, and include all the code that modifies listOfItems.

编辑:

ListOfItems似乎只能保留一本字典,这意味着你并不真正需要它。我建议使用数组而不是字典作为顶级容器,因为它会使numberOfRows更简单,但是要使用它,你需要替换而不是封装字典。

ListOfItems seems to only hold the one dictionary, which means you don't really need it. I recommended using an array instead of a dictionary as your top level container because it will make numberOfRows simpler, but to use it, you need to replace, not encapsulate, the dictionary.

如果你想继续使用字典作为顶级容器,那就没问题。您应该更改numberOfSections以计算字典中的项目数而不是listOfItems数组。 NumberOfRows应如下所示:

If you want to continue using the dictionary as the top level container that's OK. You should change numberOfSections to count the number of items in the dictionary rather than the listOfItems array. NumberOfRows should look something like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = [sectionNameArray objectAtIndex:section];  
    NSArray *array = [dictionary objectForKey:key];
    return [array count];
}

其中 sectionNameArray 是用于字典键的字符串数组(它们不必是实际的节名)。

Where sectionNameArray is an array of strings you use for dictionary keys (they don't have to be the actual section names).

编辑2:

一些事情:

1)alphabetArray有内存泄漏,你不应该保留它,因为它已经存在由alloc保留。

1) alphabetArray has a memory leak, you shouldn't retain it as it's already been retained by alloc.

2)您想要跳过的任何部分都需要从alphabetArray中删除。因此,最好从空数组开始,并在到达if语句时添加一个字母(if([charAArray count]> 0)例如)。

2) Any sections that you want to skip need to be removed from the alphabetArray. For this reason it might be better to start with an empty array and add a letter when you get to that if statement (if([charAArray count] >0) for example).

3)在cellForRow中,您似乎试图在每个表格单元格中一起显示每个字母数组中的一个项目。这是你的意图吗?您只看到最后一个值的原因是因为您不断更改标签的文本(如果charAArray有任何对象,它会覆盖值charSpecicalArray集)。我怀疑你真正想做的是使用我上面发布的代码来选择一个数组,然后使用indexPath行从该数组中选择一个项目,并使用该项目设置标签。这是正确的吗?

3) In cellForRow, you seem to be trying to display one item from each letter array all together in each table cell. Is this your intention? The reason you are only seeing the last value is because you keep changing the text of your label (if charAArray has any objects, it overwrites the value charSpecicalArray set). I suspect that what you really want to do is use the code I posted above to select one array, then use the indexPath row to select one item from that array, and set the labels using that one item. Is this correct?

编辑3:

cellForRowAtIndexPath 应该返回给定indexPath的一个单元格,而不是表格的所有单元格。我不认为你正在修改你的字母数组(你不应该这样),所以你想要的是这样的:

cellForRowAtIndexPath is supposed to return one cell given an indexPath, not all the cells for the table. I don't think you are modifying your letter arrays (you shouldn't be), so what you want is something like this:

NSString *key = [alphabetArray objectAtIndex:section];  
NSArray *array = [charDict objectForKey:key];
Char *myChar = (Char *)[array objectAtIndex:indexPath.row];

cell.textLabel.numberOfLines = 1;
cell.detailTextLabel.numberOfLines = 2;
[cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize: 12]];

cell.textLabel.text = myChar.ta;
cell.detailTextLabel.text = myChar.descriptionEN;

这应该替换所有 if([array count]> 0 ) 中的部分cellForRowAtIndexPath

这篇关于numberOfRowsInSection从最后一节开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆