1
我正在寫一個iPhone應用程序,我有一個表格視圖,它是由我從txt文件提供的信息填充的。我想分組表格(按照聯繫人的名稱來分類),但是我想選擇哪些分組放在哪個分組中。試圖將我的表格視圖與自定義分組(我的xcode 4.2 iOS 5.0)
例如,對於我的應用程序,我希望按類型(所以乳製品,農產品等)將成分分組,然後將相應的成分出現在那裏。我會怎麼做?我有這樣的代碼,目前我的表視圖:
NSString *wellBalancedIngredientFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Well Balanced Ingredients" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];
wellBalancedIngredients = [wellBalancedIngredientFileContents componentsSeparatedByString:@"(penguins)"];
wellBalancedIngredients(作爲一個陣列)是一種將包含所有的「平衡因素」的數組。我有3個其他數組,我也根據用戶選擇填充tableview。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (ingredientListChoice == 1) {
return [self.wellBalancedIngredients count];
}
else if (ingredientListChoice == 2) {
return [self.meatIngredients count];
}
else if (ingredientListChoice == 3) {
return [self.vegetarianIngredients count];
}
else {
return [self.veganIngredients count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (ingredientListChoice == 1) {
cell.textLabel.text = [self.wellBalancedIngredients objectAtIndex:indexPath.row];
}
else if (ingredientListChoice == 2) {
cell.textLabel.text = [self.meatIngredients objectAtIndex:indexPath.row];
}
else if (ingredientListChoice == 3) {
cell.textLabel.text = [self.vegetarianIngredients objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = [self.veganIngredients objectAtIndex:indexPath.row];
}
return cell;
// Configure the cell...
}
如何添加到該代碼添加分組到部分,然後能夠具有自定義標題和我根據我的選擇優先權填充每個部分。感謝您的幫助。
但後來我該如何選擇哪些項目歸入哪個部分? – 2012-02-12 19:43:53
從tableView中傳遞的indexPath:cellForRowAtIndexPath:你可以知道該部分的索引和所請求的行。 – iDifferent 2012-02-12 19:49:40