我有一个包含3个部分的表格,每个部分都有几个项目。 我需要为用户选择的每个不同的行调用特定的方法。 我该怎么做?
这是我的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
// My ARRAYS
array1 = [[NSArray alloc] initWithObjects:@"item 1", @"item 2", nil];
array2 = [[NSArray alloc] initWithObjects:@"item 1", @"item 2", nil];
array3 = [[NSArray alloc] initWithObjects:@"item 1", @"item 2", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return [array1 count];
} else if (section == 1) {
return [array2 count];
} else {
}
return [array3 count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// Define the Titles for the Sections
if (section == 0) {
return @"SECTION 1";
} else if (section == 1) {
return @"SECTION 2";
}
return @"SECTION 3";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SobreCell *cell = (SobreCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SobreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (indexPath.section == 0) {
// Section 1
// tituloLabel is a Label in the Cell
cell.tituloLabel.text = [array1 objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
// Section 2
cell.tituloLabel.text = [array2 objectAtIndex:indexPath.row];
} else {
// Section 3
cell.tituloLabel.text = [array3 objectAtIndex:indexPath.row];
}
return cell;
}
我应该使用didSelected行方法,但我不知道如何做到这一点
答案 0 :(得分:2)
使用tableView:didSelectRowAtIndexPath:
方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.section == 0)
{
// do something
}
else if (indexPath.section == 1)
{
// do something else
}
...
}
答案 1 :(得分:1)
你可以按照你在cellForRow中的方式进行:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0)
{
[self someMethod1];
}
else if (indexPath.section == 1)
{
[self someMethod2];
}
else{
[self someMethod3]
}
}
答案 2 :(得分:1)
您可以使用indexpath.row来标识当前选定的行。这是代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0)
{
if ( indexPath.row == 0 )
[self someMethod1]; // Called when first row is selected under section 0
else if ( indexPath.row == 1 )
[self someMethod2]; // Called when second row is selected under section 0
}
else if (indexPath.section == 1)
{
if ( indexPath.row == 0 )
[self someMethod3]; // Called when first row is selected under section 1
else if ( indexPath.row == 1 )
[self someMethod4]; // Called when second row is selected under section 1
}
else
{
if ( indexPath.row == 0 )
[self someMethod5]; // Called when first row is selected for remaining sections
else if ( indexPath.row == 1 )
[self someMethod6]; // Called when second row is selected for remaining sections
}
}