0

我以编程方式创建了一个 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];
}
4

1 回答 1

3

在您的 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。

于 2012-08-10T09:07:20.580 回答