我有一个分组表,如果我写:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 7){
它没有点击第7行...
我有这种UITableView(分组):
- (void)viewDidLoad {
NSArray *arrTemp1 = [[NSArray alloc]
initWithObjects:@"Scuola/Università",@"Uscita con le amiche",@"Pranzo di lavoro",@"Lavoro",@"Appuntamento",@"All'ultimo momento",nil];
NSArray *arrTemp2 = [[NSArray alloc]
initWithObjects:@"Cena con amici",@"Cena di lavoro",@"Pub/Discoteca",@"Festa elegante",@"Appuntamento",@"All'ultimo momento",nil];
NSArray *arrTemp3 = [[NSArray alloc]
initWithObjects:@"Compleanno",@"Anniversario",@"Matrimonio",@"Laurea/Promozione",@"San Valentino",@"Natale/Capodanno",nil];
NSArray *arrTemp4 = [[NSArray alloc]
initWithObjects:@"Scelta e uso fondotinta",@"Scelta dei colori",@"I pennelli",@"Come sfumare",@"Contouring",@"Labbra perfette",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arrTemp1,@"Trucco da giorno",arrTemp2,
@"Trucco da sera",arrTemp3,@"Eventi speciali",arrTemp4,@"Lezioni di base",nil];
self.tableContents =temp;
[temp release];
self.sortedKeys =[[self.tableContents allKeys]
sortedArrayUsingSelector:@selector(compare:)];
[arrTemp1 release];
[arrTemp2 release];
[arrTemp3 release];
[arrTemp4 release];
[super viewDidLoad];
}
我是否需要与== 7不同的东西?
提前谢谢
整个代码:
#import "TabellaController.h"
@implementation TabellaController
@synthesize tableContents;
@synthesize sortedKeys;
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellAccessoryDisclosureIndicator;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSArray *arrTemp1 = [[NSArray alloc]
initWithObjects:@"Scuola/Università",@"Uscita con le amiche",@"Pranzo di lavoro",@"Lavoro",@"Appuntamento",@"All'ultimo momento",nil];
NSArray *arrTemp2 = [[NSArray alloc]
initWithObjects:@"Cena con amici",@"Cena di lavoro",@"Pub/Discoteca",@"Festa elegante",@"Appuntamento",@"All'ultimo momento",nil];
NSArray *arrTemp3 = [[NSArray alloc]
initWithObjects:@"Compleanno",@"Anniversario",@"Matrimonio",@"Laurea/Promozione",@"San Valentino",@"Natale/Capodanno",nil];
NSArray *arrTemp4 = [[NSArray alloc]
initWithObjects:@"Scelta e uso fondotinta",@"Scelta dei colori",@"I pennelli",@"Come sfumare",@"Contouring",@"Labbra perfette",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arrTemp1,@"Trucco da giorno",arrTemp2,
@"Trucco da sera",arrTemp3,@"Eventi speciali",arrTemp4,@"Lezioni di base",nil];
self.tableContents =temp;
[temp release];
self.sortedKeys =[[self.tableContents allKeys]
sortedArrayUsingSelector:@selector(compare:)];
[arrTemp1 release];
[arrTemp2 release];
[arrTemp3 release];
[arrTemp4 release];
[super viewDidLoad];
}
- (void)dealloc {
[tableContents release];
[sortedKeys release];
[super dealloc];
}
#pragma mark Table Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.sortedKeys count];
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
return [self.sortedKeys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table
numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:
[self.sortedKeys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil){ cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:@"cellID"] autorelease];
}
//inseriamo nella cello l'elemento della lista corrispondente
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
return cell;
}
// Metodo relativo alla selezione di una cella
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 6){
//l'utente ha cliccato sull'elemeno iPhone, quindi carichiamo la vista relativa
detail = [[ScuolaUniversitaController alloc] initWithNibName:@"ScuolaUniversita" bundle:[NSBundle mainBundle]];
}
//Facciamo visualizzare la vista con i dettagli
[self.navigationController pushViewController:detail animated:YES]; //rilasciamo il controller
[detail release];
detail = nil;
}
@end
答案 0 :(得分:0)
if (indexPath.row == 7)
没有显示第七行显示第8行,所以你可能只有7行,而且选择类型也可能是none。所以检查一下。
编辑:
好的,您需要将if(indexPath.row == 7)
行更改为
if (indexPath.section == 1 && indexPath.row==0)
实际上这里发生了什么,你正在制作3节有6行,所以对于第七行,你可以访问第1节然后第0行用于访问第13行,你需要访问第2节和第0行。
现在我觉得你可以理解。
答案 1 :(得分:0)
至于第一组只能点击,我相信这是由于你创建表格单元格的方式。尝试替换:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:@"cellID"]
with:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cellID"]
第二:当你在每个tempArray上定义了6行时,我不明白为什么你试图在第6行(第7行)上检测点击。确保永远不会检测到点击,因此永远不会创建“详细”视图控制器。