Reputation: 8396
I'm using a UITableView
with sections to build index table, lets say that i have an Array with 500 of string data. Now i need to index the table for an easy scroll by 1 , 50 , 100 , 150 , 200 and so on until 500. so when i scroll to 50 i go to indexPath.row 50.
i really couldn't achieve it and i tried the following :
@IBOutlet weak var mytableView: UITableView!
var tableData = [String]()
var indexOfNumbers = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableData = [
// 500 lines of string array of different starting letter
]
let indexNumbers = "0 50 100 150 200 250 300 350 400 450 500"
indexOfNumbers = indexNumbers.componentsSeparatedByString(" ")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = tableData[indexPath.section]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return indexOfNumbers
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
let temp = indexOfNumbers as NSArray
return temp.indexOfObject(title)
}
Upvotes: 1
Views: 233
Reputation: 1081
As i am seeing you have 11 section with 1 row each. You can scroll to each row (as per your code the only row of the section). If you want to do what you explained in your question, separate tableData
in 50-50 cells (in array of array manner). Load 50 cells in each section, remove last section (the 500th).
You need to fix this too because componentsSeparatedByString
returns an array of separated components.
var indexOfNumbers = [NSArray]()
let indexes = "0 50 100 150 200 250 300 350 400 450 500"
indexOfNumbers = indexes.componentsSeparatedByString(" ")
Your can separate your array by subarrayWithRange
method of NSArray
class. Code in objective C.
NSMutableArray *mutA=[[NSMutableArray alloc]init];
for (int i=0; i<10; i++)
{
NSArray *halfArray;
NSRange theRange;
theRange.location = i*50;
theRange.length = [mapCat count] / 10;
halfArray = [mapCat subarrayWithRange:theRange];
[mutA addObject:halfArray];
}
Upvotes: 2