【问题标题】:cells in UITableView to new views Objective-CUITableView 中的单元格到新视图 Objective-C
【发布时间】:2012-08-10 20:59:42
【问题描述】:

我以编程方式创建了一个 UITableView,其中包含连接到情节提要中其他视图的不同单元格和部分

我想连接我的单元格,我的意思是当用户选择特定行时它应该转到新视图“您可以在故事板视图中看到它们如何相互连接”

我的问题是:

如何将这些单元格连接到我为 prepareForSegue:,viewDidLoad,didSelectRowAtIndexPath: 编写代码的视图,并且我知道我应该在 cellForRowAtIndexPath: 方法中编写连接代码,但我不知道应该如何编写它,你能帮我吗

提前致谢!

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier];
}

[[cell textLabel] setText:contentForThisRow];
return cell;

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
}

【问题讨论】:

    标签: objective-c ios xcode uitableview storyboard


    【解决方案1】:

    在您的 didSelectRowAtIndexPathMethod 中,您应该检查单击了哪个单元格,然后执行 segue:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        _selectedIndex = indexPath.row;
        [self.tableView reloadData];
        //maybe you could use a switch/case here, to assign the correct string to the segue identifier.
        switch (indexPath.row){
            case 0:
                [self performSegueWithIdentifier:@"WorkTime" sender:self];
                break;
            case 1:
                [self performSegueWithIdentifier:@"Absence" sender:self];
                break;
            case 2:
                [self performSegueWithIdentifier:@"Compensation" sender:self];
                break;
        }
    }
    

    这样,当一个单元格被选中时,它将执行segue。

    【讨论】:

    • 谢谢它的工作,但在我的一个观点中,它没有显示我的自定义单元格之一你知道是什么问题吗?
    • @justin 如果可行,您应该接受答案。如果没有出现自定义单元格,您应该提出另一个问题来解决此问题:)
    • 请你写一下 switch 语句,因为对我来说它不起作用
    • 我用开关盒写了一个小样本。行数应该适应你的表。也许您必须使用该部分而不是该案例的行。这取决于您的表格是如何填充的。
    • 它没有用,这不是答案,因为当我运行程序时它只是带来第一个视图
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多