2016-04-24 38 views
2

我是新來的swift & Xcode(教我自己通過教程和計算器)。我已經編寫了一些在TableView中添加列表的代碼,現在我正在嘗試將這些列表的地方分類。排序UITableView到部分

具體來說,我有一個ViewController,我輸入名稱,鄰居和朋友(所有字符串),這增加了一個地方到我的TableView的底部。

我想按照街區將這個地點列表分組,並且將相同街區中的所有地點一起顯示在一個節中,並使用鄰居字符串作爲節標題。

我很接近,但我沒有正確編制我的部分索引。 indexPath.section我相信是我缺少的是什麼..

到目前爲止,我在TableViewController有這樣的代碼:

// MARK: Properties 

var places = [Place]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Use the edit button item provided by the table view controller. 
    navigationItem.leftBarButtonItem = editButtonItem() 

    // Load any saved places, otherwise load sample data. 
    if let savedPlaces = loadPlaces() { 
     places += savedPlaces 
    } 

    else { 
     // Load the sample data. 
     loadSamplePlaces() 
    } 

    // Sort by neighborhood 
    places.sortInPlace { (place1, place2) -> Bool in 
     return place1.neighborhood < place2.neighborhood 
    } 
} 

// MARK: Getting Count, Number and Name of Neighborhoods 
func getCountForNeighborhood(neighborhood:String) -> Int { 
    return places.filter { (place) -> Bool in 
     return place.neighborhood == neighborhood 
    }.count 
} 

func getNumberOfNeighborhoods() -> Int { 
    var neighborhoodCount = 0 
    var neighborhoodPrev = "Not a real neighborhood" 
    for place in places { 
     if place.neighborhood != neighborhoodPrev { 
      neighborhoodCount += 1 
     } 
     neighborhoodPrev = place.neighborhood 
    } 
    return neighborhoodCount 
} 

func getNeighborhoodForSection(section:Int) -> String { 
    var previousNeighborhood:String = "Not a real neighborhood" 
    var currentIndex = -1 

    for place in places { 

     if(place.neighborhood != previousNeighborhood) { 
      currentIndex += 1 
     } 
     if(currentIndex == section){ 
      return place.neighborhood 
     } 
     previousNeighborhood = place.neighborhood 
    } 

    return "Unknown" 
} 

func loadSamplePlaces() { 

    let place1 = Place(name: "Motorino", neighborhood: "East Village", friend: "Maggles")! 
    let place2 = Place(name: "Bar Primi", neighborhood: "Lower East Side", friend: "Em")! 
    let place3 = Place(name: "El Carino", neighborhood: "Williamsburg", friend: "Ruby")! 

    places += [place1, place2, place3] 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    //alter this for To Try, Been To sections =2 
    return getNumberOfNeighborhoods() 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 

    let neighborhood = getNeighborhoodForSection(section) 
    return getCountForNeighborhood(neighborhood) 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "PlaceTableViewCell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PlaceTableViewCell 

    // Fetches the appropriate place for the data source layout. 

    let place = places[indexPath.row] 

    cell.placeLabel.text = place.name 
    cell.neighborhoodLabel.text = place.neighborhood 
    cell.friendLabel.text = place.friend 

    return cell 
} 

//Add section headers 
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return getNeighborhoodForSection(section) 

} 

我相當肯定它與我的cellForRowAtIndexPath方法的問題。我覺得我失去了一些代碼來正確索引的數據段......

的當前構建顯示器this

回答

0

你已經和部分一團糟。

在方法tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)中,您正在根據indexPath的行從places數組中獲取數據。 但是你根本不「談論」部分。這就是爲什麼你告訴數據源委託來獲取這兩個位置對象的數據,而不管它要求你的部分。 你做了一個數據結構關係的風格是這樣的:

neighbourhood1 - place1 
neighbourhood1 - place2 
neighbourhood2 - place3 
neighbourhood2 - place4 
neighbourhood3 - place5 
neighbourhood3 - place6 

相信我,這真的很難接口,該數據結構與UITableView數據源的概念,因爲它真的很難確定部分指標來自單一數據。 嘗試在這樣的樹來排列數據:

neighbourhood1: 
- place1 
- place2 
neighbourhood2: 
- place3 
- place4 
neighbourhood3: 
- place5 
- place6 

在數組的數組,然後它將是簡單的,使得所述indexPath對(部分,行)將匹配的places[section][row]的索引:

neighbourhood1: (section 0) 
- place1 (path 0.0) 
- place2 (path 0.1) 
neighbourhood2: (section 1) 
- place3 (path 1.0) 
- place4 (path 1.1) 
neighbourhood3: (section 2) 
- place5 (path 2.0) 
- place6 (path 2.1) 
0

我與其他答案同意:這將是更容易,如果你的店在首先由居委會組織了一個二維數組數據,然後將第二個,然後訪問數據爲:

let place = places[indexPath.section, indexPath.row]; 

但是,對於您的具體問題和當前代碼,我在這裏看到兩件事情。首先,您的示例數據在每個社區都有一個項目,但您的表格每個社區顯示兩行。這表示您的numberOfRowsInSection函數在我們預期爲1時返回2。請確保您的getNeighborhoodForSection正在返回正確的部分。然後看看爲什麼getCountForNeighborhood沒有爲每個部分返回1。 (另外,您可能已經知道這一點,但getNeighborhoodForSection假設您的數據總是按鄰域排序。如果您按順序插入新鄰域,則會遇到問題)。

二,cellForRowAtIndexPath您總是通過indexPath.row號碼拉數據。當您爲每個社區輸入此功能時,indexPath.row號碼將始終從0開始重新爲每個部分(鄰居)。因此,您需要從places(Motorino)中抽出第一個項目作爲每個部分的第一行。這就是爲什麼你會在每個部分看到相同的地點列表。

一旦你有每行附近的權數,在cellForRowAtIndexPath您需要:

  1. 使用getNeighborhoodForSection(index.section);
  2. 搜索獲取index.section附近通過places,直到你發現附近並保存出發點,指數,我會打電話給neighborHoodStartIndex
  3. let place = places[neighborHoodStartIndex + indexPath.row]

爲了調試的目的,我建議在每個社區製作不同數量的樣本地點。當您預計每個部分的行數不同時,發現問題模式會更容易一些。