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 で、クリックされたセルを確認し、セグエを実行する必要があります。

- (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;
    }
}

このようにして、セルが選択されると、セグエが実行されます。

于 2012-08-10T09:07:20.580 に答える