2013-05-01 313 views
0

試圖確定爲什麼我不能讓我的新「藝術」菜單在didSelectRowAtIndexPath.被選中後顯示我意識到它沒有被正確使用......另外,有沒有一種方法可以使它在兩次敲擊後顯示,還是didSelectRowAtIndexPath默認只工作一次按?didSelectRowAtIndexPath不起作用?

-(void)viewDidAppear:(BOOL)animated 
{ 
NSString *arts = @"Arts and Museums"; 

[arrayNo addObject:arts]; 

[[self myTableView] reloadData]; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
arrayNo = [[NSMutableArray alloc] init]; 
[[self myTableView] setDelegate:self]; 
[[self myTableView] setDataSource:self]; 
} 

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

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

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

    if (!cell) 
    { 
    NSLog(@"CREATING NEW CELL"); 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0]; 
    } 

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

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if ([cell.textLabel.text isEqualToString: @"Arts and Museums"]) 
    { 
    NSString *galleries = @"Art Galleries"; 
    NSString *dramatic = @"Dramatic Arts"; 
    NSString *museums = @"Museums"; 

    [arrayNo addObject:galleries]; 
    [arrayNo addObject:dramatic]; 
    [arrayNo addObject:museums]; 

    [self.myTableView reloadData]; 
    } 
} 

** * ** * ** * *** 修訂 ** * ** * ** * ** **

更新我的didSelectRowAtIndexPath與下面的代碼,我看到它是引用它,但我的新菜單項不彈出來替換舊的數組。我嘗試使用self.myTableView = nil並重新加載數據,但它會出現它沒有影響?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if ([cell.textLabel.text isEqualToString: @"Arts and Museums"]) 
    { 
     NSString *galleries = @"Art Galleries"; 
     NSString *dramatic = @"Dramatic Arts"; 
     NSString *museums = @"Museums"; 

     [arrayNo addObject:galleries]; 
     [arrayNo addObject:dramatic]; 
     [arrayNo addObject:museums]; 

     NSLog(@"LOADED"); 
    } 
} 
+0

你嘗試調試?它是否在'didSelectRowAtIndexPath:'方法內? – 2013-05-01 01:56:05

+0

如果didSelectRowAtIndexPath未觸發,則可能是您沒有正確設置TableView的委託。至於雙擊,您可能需要使用uigesturerecognizer來實現您自己的自定義操作方法來檢測水龍頭。 – ninehundredt 2013-05-01 01:58:05

+0

你還有沒有[self.myTableView reloadData];在更新的didSelectRowAtIndexPath方法的末尾? – rdelmar 2013-05-01 02:16:36

回答

0

對於您應該改變你這樣的代碼,(使用[arrayNo removeAllObjects]清除陣列)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if ([cell.textLabel.text isEqualToString: @"Arts and Museums"]) 
    { 
     NSString *galleries = @"Art Galleries"; 
     NSString *dramatic = @"Dramatic Arts"; 
     NSString *museums = @"Museums"; 

     [arrayNo removeAllObjects]; 

     [arrayNo addObject:galleries]; 
     [arrayNo addObject:dramatic]; 
     [arrayNo addObject:museums]; 

     NSLog(@"LOADED"); 
     [self.myTableView reloadData]; 
    } 
} 
+0

奇怪!這工作,但我可以看到...我忘了removeAllObjects。因此,項目必須已被添加,但因爲該數組最初被調用它不是ACTUALLY添加它們?即我必須使用'removeAllObjects'來「重置」它? – Greg 2013-05-01 02:24:01

+0

@ Thilina.Hewagama我現在唯一的問題是如何讓'didSelectRowAtIndexPath'在TWO觸摸後選擇一個單元格,與默認情況下的單元格相反。 – Greg 2013-05-01 02:29:14

+1

這將幫助你的隊友,http://stackoverflow.com/questions/1031254/how-can-i-detect-a-double-tap-on-a-certain-cell-in-uitableview – 2013-05-01 02:35:28

3

這裏是你的問題:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

這是用於製造電池的方式,但如果你想檢索現有的電池,你應該這樣做:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

此外,當你撥打這個[self.myTableView reloadData];它重新加載你所有的數據,所以你可能會抹去你所做的任何更改。