2011-10-17 30 views
0

所以項目組成員和我一直在分別研究我們的部件。我一直在研究應用程序中的一半視圖,並且他在另一半上工作。我把他的一半送給了他和他的一半,但當他試圖打開我的一半並運行它時,我們得到了不一致的結果。我們還沒有合併它們,所以它們不應該採取不同的行動。我試圖在其他幾臺電腦上測試它們,結果都一樣(與我的不同)。XCode在多臺計算機上的結果不一致

下面的代碼應該打印「CHECKED X」。 X表示在該點打勾的單元格的數量。在我的電腦上,它會打印「CHECKED 1..2..3..4」,您只能選擇最多4個,具體取決於打勾的數量。在任何其他計算機上,它會打印「CHECKED 0」,您可以隨心所欲地打勾(但不能取消打勾)。代碼滴答他們在這裏:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath]; 
     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

     if ((cell.accessoryType == UITableViewCellAccessoryNone) && [[DataContainerSingleton theDataContainerSingleton].favourites count] < 4 && ![self.tickedIndexPaths containsObject:indexPath]) 
     { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
       [self.tickedIndexPaths addObject:indexPath]; 

       [[DataContainerSingleton theDataContainerSingleton].favourites addObject:[drinks objectAtIndex:indexPath.row]]; // DOES NOT ADD AN OBJECT? 
       NSLog(@"CHECKED %d", [[DataContainerSingleton theDataContainerSingleton].favourites count]); 
     } 
     else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
     { 
      [self.tickedIndexPaths removeObject:indexPath]; 
      for (int i = 0; i < [[DataContainerSingleton theDataContainerSingleton].favourites count]; i++) 
      { 
       NSString *drinkName = [[drinks objectAtIndex:indexPath.row] name]; 
       NSString *favName = [[[DataContainerSingleton theDataContainerSingleton].favourites objectAtIndex:i] name]; 

       if ([drinkName isEqualToString: favName]) 
       { 
        cell.accessoryType = UITableViewCellAccessoryNone; 
        [[DataContainerSingleton theDataContainerSingleton].favourites removeObjectAtIndex:i]; 
        NSLog(@"UNCHECKED %d", [[DataContainerSingleton theDataContainerSingleton].favourites count]); 
       } 
      } 

     } 

} 

在我看來,我的數據容器單身人士的收藏夾陣列似乎沒有被填補。飲料陣列不是空的。

下面是DataContainerSingleton類的代碼:

DataContainerSingleton.h

#import <Foundation/Foundation.h> 


@interface DataContainerSingleton : NSObject 
{ 
    NSMutableArray *favourites; 
    NSMutableArray *drinks; 
    NSString *name; 
    int caffeine; 

    @private 

} 

@property (nonatomic, retain) NSMutableArray *favourites; 
@property (nonatomic, retain) NSMutableArray *drinks; 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, assign) int caffeine; 

+ (DataContainerSingleton *) theDataContainerSingleton; 
@end 

DataContainerSingleton.m

#import "DataContainerSingleton.h" 


@implementation DataContainerSingleton 

@synthesize favourites, drinks; 
@synthesize name, caffeine; 

static DataContainerSingleton* _theDataContainerSingleton = nil; 

+ (DataContainerSingleton*) theDataContainerSingleton; 
{ 
    if (!_theDataContainerSingleton) 
     _theDataContainerSingleton = [[DataContainerSingleton alloc] init]; 
    return _theDataContainerSingleton; 
} 

- (id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     self.favourites = [[[NSMutableArray alloc] init] autorelease]; 
    } 

    return self; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

我已經檢查了所有的依賴在節目中,沒有任何的他們在我複製的文件夾之外。我清理並清理了構建文件夾,但它仍然打印0.

有沒有人有任何想法可能導致這種不一致?提前致謝。

回答

0

我認爲「不一致的結果,」你提到可能是由於這樣的事實,你可能已經做出了delegate和你tableViewDataContainerSingletondataSource在Interface Builder的連接,但你的朋友有不同版本的.xib其中該連接不存在。這不會給出任何編譯錯誤或警告,但會將其本身表現爲一個「錯誤」,實際上並未調用委託或數據源方法。既然你可以看到日誌記錄,我會假設delegate設置正確,但dataSource不是..?

另一種選擇是,你的cellForRowAtIndexPath方法每次都會返回一個新的單元格(沒有複選標記,因此你只能檢查,但從不取消選中)。

編輯:更精確地瞭解這一點,你應該有代碼在沿着這些線路cellForRowAtIndexPath

UITableViewCell *cell; 
cell = [tableView dequeueReusableCellWithIdentifier:@"drinkCell"]; 
if(cell == nil) // we need to create a new one 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:@"drinkCell"]; 
} 

if([tickedIndexPaths containsObject:indexPath]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

否則,如果iPhone內存不足,操作系統將要求應用程序清除來自內存的任何非嚴格必要的對象,其中包括您現有的單元。因此,例如,下次您在第2行檢索單元格時,必須使用默認值創建一個新的UITableViewCell對象,這意味着None的附件。因此,每次獲取單元格時都應明確設置複選標記。

看到您的第一條評論後:好的。你應該確保你不要添加重複的indexPath。如果選擇了一行,請確保checkedIndexPaths不包含您要添加的路徑。

+0

第一個選項是不可能的,因爲它們是完全相同的副本。我有第二臺電腦,我現在正在測試它,它給我的結果與我現在使用的筆記本電腦不同。我直接複製它並打開了同一個文件的精確副本。我檢查了我的tableView的委託和數據源,他們都設置正確。 didSelectRowAtIndexPath方法實際上被調用,我用checpoint進行了檢查。 我打印出tickedIndexPaths每次我打電話上面的方法,它確實在4以上。 飲料數組被初始化。 – JheeBz

3

我給他我的一半使用版本控制軟件了一半

開始合併。你正在爲自己做一些不必要的事情。起初有一個小的學習曲線,但即使在你的第一個非平凡的項目上,它也會帶來回報。

如果肯定你有代碼庫的相同副本,那麼你也許可以縮小它執行下列操作之一:

  • 不同的工具鏈。確保你都運行相同版本的Xcode和所有SDK的相同版本等。

  • 不同的數據。從您的設備上刪除應用程序,然後重試。

  • 捆綁中的文件。通過Xcode安裝開發版本不會從軟件包中刪除舊文件。你可能在你的捆綁包中有他不存在的文件,反之亦然。從您的設備上刪除應用程序,然後重試。

  • 不同的硬件。有時候,錯誤只會在某些硬件上出現。不同的設備功能可以將相同的代碼發送到不同的路徑。你們都嘗試暫時在模擬器中進行測試。

+0

我已經檢查過XCode的版本(和內部版本)在我測試過的兩臺計算機上都是一樣的。 此外,我刪除了應用程序,它仍然執行相同的操作。 我以前沒有重新安裝過XCode,我只安裝過一次。 我們只是在iPhone模擬器中測試它,我們還沒有對真正的iPhone進行測試。 – JheeBz

相關問題