2015-04-20 34 views
0

有人可以幫助我。我將分割到的UITableView兩個registeredunregistered這是我的觀點看起來像如何在一個UITableView中創建多個列表? IOS Swift

enter image description here

,但我不知道如何繪製的json數據到這一點。這是我的數據

[ { "itemName": "Glass 101", "status": 1 }, { "itemName": "Glass 102", "status": 0 }, { "itemName": "Glass 103", "status": 0 }, { "itemName": "Glass 104", "status": 1 }, { "itemName": "Glass 105", "status": 0 }, { "itemName": "Glass 106", "status": 1 } ]

我需要做的,如果狀態等於0我把它放在not registered那麼如果1我把它registered什麼。

我現在所擁有的就是簡單的列表,一個列表,既有已註冊的,也有未註冊的列表。我想知道我怎麼能做到這一點。

任何意見或建議或鏈接都​​會有很大的幫助因爲我不知道如何開始。在此先感謝

+0

您只有兩個狀態權限(0或1)? –

+0

將您的JSON過濾爲兩個數組 - 查看「filteredArrayUsingPredicate」,然後爲每個表部分使用適當的數組 – Paulw11

+0

是先生0沒有註冊,1註冊@AkashShinde – CaffeineShots

回答

3

讓我們考慮您的數據存儲在名爲tempArray的數組中。那麼這樣做:在UITableView中的numberOfRowsInSection

NSMutableArray *registered = [[NSMutableArray alloc] init]; 
NSMutableArray *notRegistered = [[NSMutableArray alloc] init]; 
for (int i = 0 ; i < tempArray.count ; i++) { 
    if ([[[tempArray objectAtIndex:i] objectForKey:@"status"] boolValue]) { 
     [registered addObject:[tempArray objectAtIndex:i]]; 
    } 
    else{ 
     [notRegistered addObject:[tempArray objectAtIndex:i]]; 
    } 
} 

然後做這件事:cellForRowAtIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == 1){ 
     return registered.count; 
    } 
    return notRegistered.count; 
} 

那麼做到這一點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 
    } 
    if (indexpath.section == 0) { 
     cell.textLabel.text = [[notRegistered objectAtIndex:indexpath.row] objectForKey:@"itemName"]; 
    } 
    else { 
     cell.textLabel.text = [[registered objectAtIndex:indexpath.row] objectForKey:@"itemName"]; 
    } 
    return cell; 
} 
+0

這是一個非常漂亮的代碼,用於識別哪個是哪個,謝謝你的先生。然而,現在缺少的是cellForRowAtIndexPath中的代碼,我不知道如何顯示名稱 – CaffeineShots

+0

我正在添加這個。等待 – Rashad

+0

感謝您的這位先生,現在很清楚。大大幫助 – CaffeineShots

1

您可以分析您的JSON和創建兩個數組,按您的條件,status = 0,將當前索引的整個對象放在not_registered_Array中,如果是status == 1,則將當前索引的整個對象放在中

NSMutableArray *registered_Array = [[NSMutableArray alloc] init]; //Define this, out of any method, may be above `viewDidLoad` 
NSMutableArray *not_registered_Array = [[NSMutableArray alloc] init]; 
for (int i = 0 ; i < yourRespomseArray.count ; i++) { 
    if ([[[yourRespomseArray objectAtIndex:i] objectForKey:@"status"] boolValue]) { 
     [registered_Array addObject:[yourRespomseArray objectAtIndex:i]]; 
    } 
    else{ 
     [not_registered_Array addObject:[yourRespomseArray objectAtIndex:i]]; 
    } 
} 

從這2陣列,可以返回行的數目爲每部,並設置從該小區的值。

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 2 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    println(prefs.valueForKey("CAT_ID")) 
    if (section == 0){ 
     return not_registered_Array.count 
    }else{ 
     return registered_Array.count 
    } 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell : UITableViewCell = ... 
    if (section == 0){ 
     let dicObject:NSDictionary = not_registered_Array.objectAtIndex(indexPath.row) as! NSDictionary 
     cell.textLabel?.text = dicObject.valueForKey("itemName") 
    }else{ 
     let dicObject:NSDictionary = registered_Array.objectAtIndex(indexPath.row) as! NSDictionary 
     cell.textLabel?.text = dicObject.valueForKey("itemName") 
    } 
    return cell 
} 

HTH,享受編碼!

+0

這就是我錯過了!感謝你的大力幫助。 – CaffeineShots

+0

- 這應該得到一個加號太寫在迅速 – CaffeineShots

2

實現此數據源方法。

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

使用filteredArrayUsingPredicate獲得兩個不同的陣列對象中的一個將是狀態0和一個將是狀態1

然後實現此方法。

-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section{ 
    if(section == 0){ 
    return [statusZero count] 
    } 
    else{ 
    return [statusOne count] 
    } 
} 

最後用這種方法補充細胞

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
if(indexPath.section == 0){ 
    // add status 1 array cell 
} 
else{ 
    // add status 0 array cell 
} 
return cell 
} 
+0

謝謝你的答案先生。 – CaffeineShots

0

我真的不知道,如果你無法將JSON解析到一個對象,或者你不知道如何分割一個UITableView在多個部分。

根據對象的用法,您需要解析JSON並使用Dictionary中的值實例化對象(數組中的每個記錄都可以簡單適合於Dictionary<String, AnyObject>)。例如,看到以下內容:

http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial

我不是說這是JSON解析爲一個對象的最佳方式,但一個方法,讓你開始。使用filteredArrayUsingPredicate將數組篩選到兩個數組中。因此,像這樣:

let myCompleteArray = {}; 
var registered = myCompleteArray.filter ({ $0.status == true }); 
var unregistered = myCompleteArray.filter({ $0.status == false }); 

然後,你需要實現你UITableViewDataSource,像這樣的東西:

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 2; 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if (section == 0) { 
     return registered.count; 
    } 
    else { 
     return unregistered.count; 
    } 
} 

當然,你需要改變你的cellForRowAtIndexPath檢查哪個部分被渲染(只如上面的numberOfRowsInSection

+0

這條線很聰明'myCompleteArray.filter({$ 0.status == true})'感謝這個 – CaffeineShots

相關問題