2014-02-26 24 views
-3

我必須得到IndexPath值到NSMutableArray。當一個特定的單元格被選中時,我必須做一個操作它的IndexPath存儲在NSMutableArray中,並且特定的單元格高度正在增加。當我再次按下那個單元格時,特定的單元格高度正在下降。我怎麼可能以及我必須提出什麼樣的條件?如何獲得indexPath值到NSMutableArray?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"%@",indexPath); 
    selectIndexPathCell = indexPath; 
    NSIndexPath *previousSelectedIndexPath = selectIndexPathCell; 
    selectIndexPathCell = indexPath; 

    if (previousSelectedIndexPath) {    
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousSelectedIndexPath] 
             withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectIndexPathCell] 
       withRowAnimation:UITableViewRowAnimationAutomatic]; 

    self.selectedRow = sections_main[indexPath.section][@"content"][indexPath.row]; 
    [tblPeople beginUpdates]; 

    [tblPeople endUpdates]; 
} 

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(selectIndexPathCell != nil && [selectIndexPathCell compare:indexPath] == NSOrderedSame){ 
     return 80; 
      }else{ 
       return 60.0; 
} 

}

回答

0
Take a global variable ex: Int selectedIndex; and now in didselectrow write this 
-(void)viewDidLoad 
{ 
[super viewDidLoad]; 
selectedIndex=50; // please assign it as your arraycount+1; which you are using to display on tableview 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if(selectedIndex==indexPath.row) 
{ 
    // if this is true you can do decrease your cell height 
selectedIndex=50; // please assign it as your arraycount+1; 
} 
else 
{ 
    // user can select one row and he can select another row without minimizing the previous one so you need to minimize the previous and increase new one. by using selectedIndex decrease cell height and by using indexPath.row increase height of cell 
selectedIndex=indexPath.row; 

} 
[tableview reload]; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(selectedIndex==indexPath.row) 
{ 
    return 60; 
} 
else 
{ 
return 30; 
} 

} 
+0

我應該在heightForRowAtIndexPath中寫什麼?是否有任何代碼檢查或其他 – Ankit

+0

@Ankit請參閱我已更新它的回答 –

+0

仍然無法在我的代碼中工作。 – Ankit

2

我的理解,你要根據表格視圖單元格高度限制條件。您可以通過將相應單元的rowIndex保存在array中,並通過使用reloadData來管理它,並且可以在heightForRowAtIndexPath委託下管理功能高度,單元格高度將根據數組值進行更改。看看我的建議here

希望它能幫助!

編輯:(未經測試版本給你一個想法)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(![arrayOfExpandedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]]) 
    { 
     [arrayOfExpandedCellIndexes addObject:[NSNumber numberWithInteger:indexPath.row]]; // add if it does not exist 
    } 
    else 
    { 
     [arrayOfExpandedCellIndexes removeObject:[NSNumber numberWithInteger:indexPath.row]]; // remove if it exists 
    } 

    [tableView reloadData]; // reload data so height would adjust in `heightForRowAtIndexPath` 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([arrayOfExpandedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]])  
     return EXTENDED_CELL_HEIGHT; // Macro : #define EXTENDED_CELL_HEIGHT 230.0f 
    else 
     return NORMAL_CELL_HEIGHT;  // Macro : #define NORMAL_CELL_HEIGHT 100.0f 
} 

假設arrayOfExpandedCellIndexesNSMutableArray保持indicies。

+0

當第一次用戶點擊單元格的時候,我們必須做出擴展和縮小的單元格,當時單元格正在擴展,第二次單元格縮小的時候用戶將單擊那個單元格。僅在用戶選擇的特定小區上。所以,我必須通過NSMutableArray來管理它。我如何將選定的索引存儲在NSMutableArray上並重新加載特定的Cell。 – Ankit

+0

使用委託(如果自定義單元格)從NSMutableArray中添加/刪除索引並調用'[yourTableView reloadData]',所以當數據重新載入花費正確的高度時,無法重新加載'特定單元格'。因此,在開始時,單元格索引不會在數組中,一旦用戶點擊單元格,didSelectRowAtIndexPath將調用數組中的存儲值(如果不存在),並在存在時移除它們。現在希望它清楚。 – NeverHopeless

+0

array1 = [[tableView cellForRowAtIndexPath:indexPath] mutableCopy];現在我得到特定的indexpath的數據? – Ankit

相關問題