2016-04-24 57 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]

为了调试的目的,我建议在每个社区制作不同数量的样本地点。当您预计每个部分的行数不同时,发现问题模式会更容易一些。