我有一个包含 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 行方法,但我不知道该怎么做
最佳答案
利用 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
}
...
}
关于ios - 为不同的行调用不同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24143466/