2012-05-02 15 views
1

行が1より大きい場合にクラッシュするuitableviewがあります。それらは配列内の2つのオブジェクトなので、意味をなさない。オブジェクト1では完全に動作しますが、2ではできません。それを確認してください:行数が1より大きい場合、UITableViewがクラッシュする

エラー:スレッド1のとき、私は[self.tableview reload data]を呼び出します。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 
*** First throw call stack: 
(0x13dc022 0x156dcd6 0x13c8644 0x44cea5 0x2591a8 0x204688 0x206862 0xb466d 0xb4167 0x2a42 0x9a5f 0xa2755e 0x96463e 0x95d1e7 0x95ceea 0x9ed0ad 0x3da2330 0x3da4509 0x1313803 0x1312d84 0x1312c9b 0x12c57d8 0x12c588a 0x26626 0x20ed 0x2055) 
terminate called throwing an exception 
+0

を?クラッシュレポートとは何ですか? – jrturton

+0

あなたは、tableViewをリロードするとクラッシュすると言っています。まず、[self.tableview reloadData]であり、[self.tableview reload data]ではありません。また、reloadDataメソッドを呼び出す場所を貼り付けることもできます。 – azamsharp

答えて

1

使用 return [pro.matches count]

の代わりに、行方法のあなたの番号に

+0

まだ動作しません – michaela

2

アプリのクラッシュが原因tableView:numberOfRowsInSection

であなただけの1を返すリターン1あなたは[pro.matches count]

を返す必要があります
+0

私はそれもクラッシュする – michaela

+0

さらにいくつかの変更が必要です。回答者 – Saad

2

にif([pro.matche S・カウント]> 0)文を使用するには、objectAtIndex アクセス:1を - マッチ配列が1つのオブジェクトが含まれている場合、それはあなたのアプリケーションがクラッシュしますインデックス0、アクセスするインデックス1を有しています。

+0

いずれかの方法でクラッシュを参照してください... – michaela

1

戻り、ここで配列サイズ:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [pro.matches count]; 
} 

if文を削除し、ただでそれを置き換える:

cell.textLabel.text = [pro.matches objectAtIndex:indexPath.row]; 

indexPath.rowはゼロから始まる、現在の行インデックスです。

+0

はい良いがクラッシュします – michaela

+0

次に別の問題があります。 –

+0

いいえ.......本当に?冗談だよ – michaela

1

配列内の文字列が正しく作成され、初期化されているかどうか確認しましたか?

  1. は、あなたの配列が良いですし、あなたがそれらを参照するとき、配列や、配列内のオブジェクトのいずれも割り当て解除されたことを確認してください:私はあなたがチェックする必要があるいくつかの場所があると思います。あなたの-numberOfRowsInSectionで

  2. 、それは リターン[pro.matchesカウント]あなたの-cellForRowAtIndexPathで

  3. する必要があり、それは cell.textLabel.text = [pro.matches objectAtIndexする必要があります:indexPath.row ];

私はデモプログラムを書いて試してみてください。それはまだそれから、ここですべてのコードをポストクラッシュした場合

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> 
{ 
    NSArray *list; 
} 

@property(nonatomic,retain) NSArray *list; 
@end 

とviewController.mで、

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize list; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSArray *tempArray = [[NSArray alloc] initWithObjects:@"a",@"b", nil]; 
    self.list = tempArray; 
    [tempArray release]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

/******************************************************************************** 
******************** UITableViewDataSource Protocol Methods ******************** 
********************************************************************************/ 

#pragma mark - UITableViewDataSource Protocol Functions 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    //NSLog(@"%i", indexPath.row); 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // configure your cell here... 
    if ([list count] >0) 
    { 
     cell.textLabel.text = [list objectAtIndex:indexPath.row]; 
    } 

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    return cell; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return list.count; 
} 

/******************************************************************************** 
******************** UITableViewDelegate Protocol Methods ********************** 
********************************************************************************/ 

#pragma mark - UITableViewDelegate Protocol Functions 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{} 
@end 
+0

なぜ[pro.matches count-1]であり、[pro.matches object at index:index path.row? – michaela

+0

クラッシュするとエラーレポートが表示されます – sandy

+0

***キャッチされない例外 'NSRangeException'、理由: '*** - [__ NSArrayI objectAtIndex:]:境界1を超えるインデックス1' ' ***最初に投げコールスタック: (0x13dc022 0x156dcd6 0x13c8644 0x44cea5 0x2591a8 0x204688 0x206862 0xb466d 0xb4167 0x2a42 0x9a5f 0xa2755e 0x96463e 0x95d1e7 0x95ceea 0x9ed0ad 0x3da2330 0x3da4509 0x1313803 0x1312d84 0x1312c9b 0x12c57d8 0x12c588a 0x26626 0x20ed 0x2055) は例外 – michaela

0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"rigth not %i", [pro.matches count]); 

    return [pro.matches count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    //NSLog(@"%i", indexPath.row); 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // configure your cell here... 
    if ([pro.matches count] >0) { 
     cell.textLabel.text = [pro.matches objectAtIndex:indexpath.row]; 
    } 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    return cell; 
} 

:私はviewController.hにおける単一のビューアプリを作成しました。

0

あなたはこれを理解しましたか?私は同じ問題を抱えていた、と私はここに掲載ソリューションとそれを解決することになった:要するにhttps://stackoverflow.com/a/8096988/810360

、私は、静的から動的に私のテーブルを設定します。

それはクラッシュしない

UITableView Properties

関連する問題