1

文件:ContactsViewController.m

在这个文件中,我使用 didSelectRowAtIndexPath 方法来推送一个新的视图控制器,以显示有关在表视图控制器上按下的名称的信息。将显示名称信息的视图控制器正在 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)")

是 0。这告诉我我没有捕获被点击的名称的索引。

myStoryboard的图像 最底部的场景是我试图更改名字标签以显示被点击的单元格名称的场景。

单击单元格时,我还无法更改此标签的标题。仅使用 swift 文件(通过观看大量视频)时,我已经能够实现此功能。我确信目标 C 文件中缺少一个关键概念,因此非常感谢任何建议和/或指针。如果需要任何其他详细信息,请告诉我!

谢谢。

4

1 回答 1

0

似乎didSelectRowAtIndexPath:您正在将 a 推ContactsViewController送到导航堆栈,如果我理解正确,它应该是ExistingContactViewController. 此外,您应该在将其推送到导航堆栈之前(在执行之前)设置它的contactsObject属性,否则它的值将始终是新 的,这可能会导致问题。我希望这有帮助!ExistingContactViewControllerviewDidLoadContactsViewController

于 2018-10-22T19:19:48.667 回答