11

Tôi có chế độ xem bảng bên trong Trình điều khiển chế độ xem. Tôi có thể điền tất cả thông tin của mình vào trong chế độ xem bảng. Tuy nhiên tôi là một chút bị mất cho việc thiết lập các quan điểm chi tiết. Tôi tin rằng mỗi tế bào bảng cần một segue để xem từng chi tiết nhưng không hoàn toàn chắc chắn.Đẩy vào Chế độ xem chi tiết từ ô xem bảng bằng cách sử dụng Bảng phân cảnh Xcode

Đây là mã của tôi. Tôi đang thiếu gì để hoàn thành phân đoạn từ chế độ xem bảng đến các chế độ xem chi tiết? Mã số:

.h 

@interface DetailViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> 
{ 
    IBOutlet UITableView *myTable; 
    NSMutableArray *contentArray; 
} 

@property (strong, nonatomic) IBOutlet UITableView *myTable; 

.m 


- (void)viewDidLoad 
{ 
    contentArray = [[NSMutableArray alloc]init]; 
    [contentArray addObject:@"Espresso"]; 
    [contentArray addObject:@"Latte"]; 
    [contentArray addObject:@"Capicino"]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

//Table Information 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [contentArray count]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if([[contentArray objectAtIndex:indexPath.row]isEqualToString:@"EspressoViewController"]) 
    { 
     EspressoViewController *espresso = [[EspressoViewController alloc]initWithNibName:@"EspressoViewController" bundle:nil]; 
     [self.navigationController pushViewController:espresso animated:YES]; 
    } 
    else if ([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"Latte"]) 
    { 
     LatteViewController *latte = [[LatteViewController alloc] initWithNibName:@"Latte" bundle:nil]; 
     [self.navigationController pushViewController:latte animated:YES]; 
    } 

} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    [self tableView:tableView didSelectRowAtIndexPath:indexPath]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"]; 
    } 

    NSString *cellValue = [contentArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.textLabel.font = [UIFont systemFontOfSize:16]; 
    cell.detailTextLabel.text = @"Hot and ready"; 

    UIImage *image = [UIImage imageNamed:@"coffeeButton.png"]; 
    cell.imageView.image = image; 

    cell.textLabel.text = [contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

Trả lời

17

Tôi nghĩ bạn đã làm điều này quá phức tạp. Đừng lo, tôi cũng thường làm như vậy.

Đầu tiên, bằng cách gửi tableView:didSelectRowAtIndexPath: từ trong vòng tableView:accessoryButtonTappedForRowAtIndexPath: không có sự khác biệt giữa hai phương pháp. Khai thác ô, hoặc nút phụ kiện của nó thực hiện cùng một hành động. Nếu bạn không cần nút phụ kiện để thực hiện một hành động khác với thao tác chạm vào chính ô đó, hãy xóa nó đi.

Thứ hai, nếu bạn đang sử dụng bảng phân cảnh, bạn không cần phân bổ/initWithNib cho bộ điều khiển chế độ xem của mình. Thay vào đó, hãy sử dụng phân đoạn. Nếu bạn làm điều này thông qua các kịch bản, bạn cũng không cần phải lập trình đẩy viewControllers vào navigationController bạn

Xây dựng kịch bản của bạn đầu tiên:

  1. Kéo ra một UITableViewController. Hãy chắc chắn rằng bạn thiết lập lớp của UITableViewController mà bạn đã kéo ra "DetailViewController" của riêng bạn bằng cách sử dụng cửa sổ thanh tra ở bên phải.
  2. Sau đó chọn điều khiển này và sử dụng các menu chọn "biên tập ->Nhúng Trong ->Navigation khiển".
  3. Tiếp theo, kéo ra ba UIViewControllers chung. Đặt lớp của một thành "LatteViewController", một lớp khác là "EspressoViewController", và thứ ba là "CapicinoViewController" (sử dụng trình kiểm tra lần nữa).
  4. Điều khiển + kéo từ UITableViewController qua từng chế độ xemCác điều khiển này và chọn PUSH.
  5. Nhấp vào vòng tròn nhỏ nằm trên mũi tên giữa UITableViewController của bạn và từng trong số các viewControllers này. Trong thanh tra (ở phía bên phải), đặt tên riêng cho từng phân đoạn trong trường Mã định danh. Bạn sẽ cần phải nhớ tên này cho mã của bạn. Tôi sẽ đặt tên cho chúng là "EspressoSegue", "LatteSegue" và "CapicinoSegue". Bạn sẽ thấy lý do tại sao trong mã bên dưới.

Sau đó đặt đoạn mã sau vào UITableViewController của bạn:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 

//Build a segue string based on the selected cell 
NSString *segueString = [NSString stringWithFormat:@"%@Segue", 
         [contentArray objectAtIndex:indexPath.row]]; 
//Since contentArray is an array of strings, we can use it to build a unique 
//identifier for each segue. 

//Perform a segue. 
[self performSegueWithIdentifier:segueString 
          sender:[contentArray objectAtIndex:indexPath.row]]; 
} 

Làm thế nào bạn thực hiện phần còn lại là tùy thuộc vào bạn. Bạn có thể muốn triển khai prepareForSegue:sender: trong UITableViewController của mình và sau đó sử dụng phương thức đó gửi thông tin qua segue.destinationViewController.

Lưu ý rằng tôi đã chuyển chuỗi từ contentArray của bạn làm người gửi cho khoảng cách đó. Bạn có thể vượt qua bất cứ điều gì bạn thích. Chuỗi xác định ô có vẻ như thông tin hợp lý nhất để vượt qua, nhưng sự lựa chọn tùy thuộc vào bạn.

Mã được đăng ở trên phải thực hiện điều hướng cho bạn.

Các vấn đề liên quan