簡體   English   中英

UITableVIew Objective-C中單元格的順序不正確

[英]Incorrect order of cells in UITableVIew objective-c

在用單元格填充表格視圖時遇到問題。

有我的數組,我的細胞應該以此數組開始

items = [NSMutableDictionary dictionaryWithObjectsAndKeys:
             @"http://feeds.test.com/home", @"Home",
             @"http://feeds.test.com/publications", @"Refcardz and Guides",
             @"http://feeds.test.com/agile", @"Agile Zone",
             @"http://feeds.test.com/big-data", @"Big Data Zone",
             @"http://feeds.test.com/cloud", @"Cloud Zone",
             @"http://feeds.test.com/database", @"Database Zone",
             @"http://feeds.test.com/devops", @"DevOps Zone",
             @"http://feeds.test.com/integration", @"Integration Zone",
             @"http://feeds.test.com/iot", @"IoT Zone",
             @"http://feeds.test.com/java", @"Java Zone",
             @"http://feeds.test.com/mobile", @"Mobile Zone",
             @"http://feeds.test.com/performance", @"Performance Zone",
             @"http://feeds.test.com/webdev", @"Web Dev Zone",
             nil];

因此,創建的單元格的順序應如下所示:

主頁-Refcardz和指南-敏捷區域-大數據區域-雲區域-數據庫區域-DevOps區域-集成區域-IoT區域-Java區域-移動區域-性能區域-Web開發區域

以這種方式訂購的BUT單元格在此處輸入圖像描述

這是我的cellForRowAtIndexPath,如果有幫助的話:

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

    NSString *curName = [webSitesNames objectAtIndex:indexPath.row];
    cell.textLabel.text = [curName copy];
    if([self.cellSelected containsObject:indexPath]){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

我真的不知道為什么會有這種奇怪的行為。 如何改進我的代碼以像查看數組一樣依次查看所有單元格?

您正在使用字典,而字典不是有序集合。 您必須改為使用數組。 而是將項目轉換為數組。

另一種選擇是在現有字典中添加另一級別的字典,並使用諸如“ 0”,“ 1”,“ 2”等鍵。

例如,項目= [NSMutableDictionary dictionaryWithObjectsAndKeys:@ {@“ url”:@“ http://feeds.test.com/home”,@ “標題”:@“ Home”},@“ 0”,@“ url”: @“ http://feeds.test.com/publications”,@ “標題”:@“ Refcardz和指南”},@“ 1” .....

基本上,您將索引用作字典的鍵,而值是另一個包含url和title鍵的字典。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM