1

ファイル: ContactsViewController.m

このファイルでは、didSelectRowAtIndexPath メソッドを使用して新しいビュー コントローラーをプッシュし、テーブル ビュー コントローラーで押された名前に関する情報を表示しています。名前に関する情報を表示するView ControllerはSwiftで実装されています。このコードで参照している部分は、didSelectRowAtIndexPath メソッドの次の行にあります。

_myIndex = indexPath.row;

indexPath.row は、Table View Controller でタップされた名前のインデックスを返す必要があると思います。

#import "ContactsViewController.h"
#import "Contacts-Swift.h"

@interface ContactsViewController ()

@property (nonatomic, readwrite, strong) NSMutableArray* contacts;

@property (nonatomic, readwrite) NSInteger myIndex;

@end

@implementation ContactsViewController



-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        /*NSArray *contactArray = @[@"Johnny Appleseed", @"Paul Bunyan", @"Calamity Jane"];
        _contacts = [NSMutableArray arrayWithArray:contactArray];*/
        /*Contact *c1 = [[Contact alloc] initWithName: @"Johnny"];
        Contact *c2 = [[Contact alloc] initWithName: @"Paul Bunyan"];
        Contact *c3 = [[Contact alloc] initWithName: @"Calamity Jane"];*/
       // _contacts = [NSMutableArray arrayWithArray: @[c1, c2, c3]];
        self.contacts = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.tableView registerClass: [UITableViewCell class]
           forCellReuseIdentifier:@"UITableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
    return self.contacts.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    Contact *contact = self.contacts[indexPath.row];
    cell.textLabel.text = contact.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContactsViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:@"the"];
    [self.navigationController pushViewController:viewController animated:YES];
    _myIndex = indexPath.row;
}

ファイル: ContactsViewController.h

これは、swift ファイルで作業するときに目的の C メソッドと変数にアクセスするために使用しているヘッダー ファイルです。(私はオブジェクティブ C にあまり詳しくないので、この実装が問題の原因である可能性が高いです)。

#import <UIKit/UIKit.h>

@interface ContactsViewController : UITableViewController <UITableViewDelegate>

@property (nonatomic, readonly) NSInteger myIndex;

@property (nonatomic, readonly, strong) NSMutableArray* contacts;

@end

ファイル: ExistingContactViewController.swift

ExistingContactViewController では、firstName ラベルを、indexPath.row の連絡先配列 (ContactsViewController.m ファイル内) に存在するテキストと等しく設定しようとしています。

import UIKit

@objc class ExistingContactViewController: UIViewController {

    var contactsObject = ContactsViewController()
    @IBOutlet weak var firstName: UILabel!
    @IBOutlet weak var lastName: UILabel!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(contactsObject.myIndex)")
        firstName.text = contactsObject.contacts[contactsObject.myIndex] as? String
    }

テーブルビューコントローラーに追加された名前をクリックすると、これまでに印刷された唯一のインデックス

print("\(contactsObject.myIndex)")

これは、タップされた名前のインデックスをキャプチャしていないことを示しています。

myStoryboardの画像 一番下のシーンは、タップされたセルの名前を表示するために First Name ラベルを変更しようとしているシーンです。

セルをクリックしたときに、このラベルのタイトルを変更できませんでした。Swiftファイルのみを使用する場合(多数のビデオを見ることにより)、この機能を実装できました。客観的なCファイルに欠けている重要な概念があると確信しているので、提案やポインタは大歓迎です。追加の詳細が必要な場合はお知らせください。

ありがとう。

4

1 に答える 1