2011-09-12 40 views
0

我有一個indexPath.row是1並記錄1(當使用NSLog時)。如果我叫indexPath.row-1(應返回0),它返回4294967295indexPath.row-1是4294967295

我試圖返回objectAtIndex:indexPath.row-1,但那個時候我拿到4294967295

任何想法?

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    Singleton *singleton = [Singleton sharedSingleton]; 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    if ([[prefs objectForKey:@"isYes"]boolValue] == 1 && randomMarker != 100) 
    { 
     //sets cell image 
     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)]; 
     imgView.image = [UIImage imageNamed:@"stackoverflow.png"]; 
     cell.imageView.image = imgView.image; 

     //sets cell text 
     cell.textLabel.text = @"Text"; 
     self.checkedInCount == 100; 
    } 
    else if ([[prefs objectForKey:@"isYes"]boolValue] == 1 && randomMarker == 100) 
    { 
     //gets cell and cleans up cell text 
     NSLog(@"%@", indexPath.row); 
     NSString *title = [[[singleton linkedList]objectAtIndex:(indexPath.row-1)]objectForKey:@"desc"]; 
+1

我的猜測是,你實際上試圖得到objectAtIndex:-1,這是無稽之談。將代碼發佈到正在發生的位置以獲得更具體的幫助。 – PengOne

+0

NSString * tempDesc = [[[singleton linkedList] objectAtIndex:indexPath.row-1] objectForKey:@「desc」]; – Baub

+0

它在這條線上崩潰,聲明索引4294967295超出範圍(0,0) – Baub

回答

10

當你試圖給一個unsigned int(NSUInteger)爲負值,它往往會返回一個非常大的正值代替。

要調用

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:indexPath.row-1]objectForKey:@"desc"]; 

indexPath.row具有價值0,所以翻譯是:

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:-1]objectForKey:@"desc"]; 

由於objectAtIndex:接受一個無符號整數作爲參數,-1被轉換成的垃圾值4294967295

要避免此問題,請不要從0中減去1,方法是先檢查indexPath.row是否爲正數。


這裏的另一個問題:

NSLog(@"%@", indexPath.row); 

這應改爲閱讀:

NSLog(@"%u", indexPath.row); 
+0

indexPath.row的值不爲0,它的值爲1.如果我使用NSLog轉儲indexPath.row,它將記錄1. – Baub

+0

@James,調用'-tableView:tableView cellForRowAtIndexPath:'* *每個**有效的'indexPath',從部分'0'和行'0'開始。 – PengOne

+0

在我的實際代碼中,這行在一個if語句中,除非indexPath> 0,否則不會被調用。 :( – Baub

0
NSLog(@"%@", indexPath.row); 

您shold使用%d的整數indexPath.row將返回一個整數

使用NSLog(@「%d」,indexPath.row);

相關問題