ios - 为不同的行调用不同的方法

标签 ios uitableview didselectrowatindexpath

我有一个包含 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/

相关文章:

ios - 在文本字段之间切换时,自定义 UITableViewCell 中的 UITextField 无法正确成为第一响应者

ios - UITableViewController 加载方法的逻辑顺序是什么?

在 indexPath.row 的数组中快速打印元素

ios - 长按后调用 didSelectRowAtIndexPath

ios - "Authentication Required"与 NSURLSession。用户/ key 正确

xamarin.ios - 如何设置 Monotouch 对话框部分的样式?

iphone - 来自ImagePicker的图像时无法检测面部特征

swift - 如何防止 UIDatePicker 被多次初始化

ios - 选项卡 View 的 3D 触摸快捷方式,然后执行 segue

ios - Autolayout 需要负常量才能与 superview 对齐?