【问题标题】:Passing data to the detail view in UITableView将数据传递给 UITableView 中的详细视图
【发布时间】:2012-10-29 04:59:59
【问题描述】:

我想通过在 UITableView 和详细视图之间传递数据来打印详细视图中 label.text 上的 indexPath.row 编号,但我无法正确执行。

ViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
    NSArray *tableItems;
    NSArray *images;

}

@property (nonatomic,retain) NSArray *tableItems;
@property (nonatomic,retain) NSArray *images;

@end

ViewController.m

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tableItems,images;
- (void)viewDidLoad
{
    [super viewDidLoad];
    tableItems = [[NSArray alloc] initWithObjects:@"Item1",@"Item2",@"Item3",nil];
    images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"clock.png"],[UIImage imageNamed:@"eye.png"],[UIImage imageNamed:@"target.png"],nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    { 
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        UIView *v = [[UIView alloc] init];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
        //changing the radius of the corners
        //cell.layer.cornerRadius = 10;

    }

    //Set the image in the row
    cell.imageView.image = [images objectAtIndex:indexPath.row];

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.backgroundColor = [ UIColor greenColor];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    [secondViewController printRowNumber:indexPath.row+1];

    NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];

    secondViewController.selectedRow = selectedRow;

    //[self.navigationController pushViewController:secondViewController animated:YES];
    [self presentViewController:secondViewController animated:YES completion:NULL];
}
@end

SecondViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"


@interface SecondViewController : UIViewController{
    IBOutlet UILabel *lbl;
    NSString *selectedRow;

}
@property (nonatomic,retain) UILabel *lbl;
@property (nonatomic,retain) NSString *selectedRow;

- (IBAction)goBack:(id)sender;

-(void) printRowNumber:(int)num;
@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize lbl,selectedRow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self changeLabel:@"Hello"];
    //lbl.text = @"Hello";
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goBack:(id)sender{

    //ViewController *firstViewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    //firstViewController.delegate = self;
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void) printRowNumber:(int)num{
    lbl.text = selectedRow;
    NSLog(@"%@",lbl.text);
    NSLog(@"%d",num);
}
-(void) changeLabel:(NSString*)str{
    lbl.text = str;
    //lbl.text = @"Hello";
}
@end

不知道如何解决这个问题..需要一些帮助..

【问题讨论】:

  • 如果你放断点,它会转到你的printRowNumber:方法吗?
  • 另外,我认为completion应该是nil in [self presentViewController:secondViewController animated:YES completion: nil]`;
  • 使用NULL并没有错,只是发送消息到nil有效,而不是NULL。看看这个 - stackoverflow.com/questions/5766208/…

标签: iphone ios uitableview


【解决方案1】:

下面,你需要更改一点代码。

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

  SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

  NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];

  secondViewController.selectedRow = selectedRow;

  //[self.navigationController pushViewController:secondViewController animated:YES];
  [self presentViewController:secondViewController animated:YES completion:NULL];

  //this is the change you should do.
  [secondViewController printRowNumber:indexPath.row+1];

}

【讨论】:

  • @lakesh 实际上您在将任何值传递给secondViewController.selectedRow = selectedRow 之前调用了[secondViewController printRowNumber:indexPath.row+1];。正如Challenger 解释的那样。
  • 还有一个问题:当我点击第 1 项时,它显示第 1 项?如何打印点击的表格单元格的索引值?
  • @lakesh 只需更改一段代码[secondViewController printRowNumber:indexPath.row];
  • 嘿@lakesh 只是更改您传递给printRowNumber 方法的值。您应该以这种方式进行。 [secondViewController printRowNumber:indexPath.row];
  • @lakesh 不,它们是不同的,看到您使用的是[secondViewController printRowNumber:indexPath.row+1],我建议您使用[secondViewController printRowNumber:indexPath.row],您正在将+1 值添加到 printNumber 方法
【解决方案2】:

我建议你只将当前视图控制器的值传递给下一个视图控制器,不要从当前控制器调用下一个控制器的功能。这将导致对象的释放。

只需传递值并立即推送该视图控制器并享受该值。希望对你有效。

【讨论】:

  • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; secondViewController.selectedRow = [tableItems objectAtIndex:indexPath.row+1]; //[self.navigationController pushViewController:secondViewController animated:YES]; } **现在在你的 secondviewcontroller 的 viewdidload 函数中调用那个打印数字函数”试试看。
  • 啊..明白..意思是说:从它自己的viewcontroller而不是parentViewController调用函数。但是我有一个问题:既然printNumber有一个叫num的参数,我怎么把它传给SecondViewController?
  • 您已经在 secondviewcontroller 的 selectedRow 属性中拥有该值,不是吗?
  • 的意思是说,我应该不带参数直接传吗?好的,假设我没有 selectedRow 属性,我如何传递参数?很抱歉打扰您...有兴趣了解更多信息...
  • 好的,我明白了,您需要在 secondviewcontroller 中再声明一个属性来存储行号。正如我所看到的,第二页需要两个值,然后您需要在第二页上有两个变量并将值传递给它们。然后,您可以从函数中删除该传递参数。
【解决方案3】:
[secondViewController printRowNumber:indexPath.row+1];
NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];
secondViewController.selectedRow = selectedRow;

如果你在你的 didSelect 方法中看这三行代码,你调用方法 printRowNumber 没有设置你的 selectedRow 字符串,所以它不起作用,尝试切换顺序为:

NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];
secondViewController.selectedRow = selectedRow;

[secondViewController printRowNumber:indexPath.row+1];

【讨论】:

    【解决方案4】:

    在 SecondViewController 的 viewDidLoad 中调用 -(void) printRowNumber:(int)num 方法,并从 firstViewController 的 rowDidSelect 中删除 [secondViewController printRowNumber:indexPath.row+1];

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self printRowNumber:[selectedRow intValue]];
    }
    

    试试这个可能对你有帮助....

    【讨论】:

      【解决方案5】:
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      
          SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
      
          NSString *selectedRow = [tableItems objectAtIndex:indexPath.row+1];
      
          secondViewController.selectedRow = selectedRow;
      
          [self presentViewController:secondViewController animated:YES completion:^{
              [secondViewController printRowNumber:indexPath.row+1];
          }];
      }
      

      在您呈现控制器后,将创建 SecondViewController 中的视图。 所以如果你在展示控制器之前调用 -printRowNumber: ,你的标签仍然没有被创建。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-10
        • 1970-01-01
        • 2014-01-07
        • 2014-05-23
        • 2023-03-22
        • 2023-04-08
        相关资源
        最近更新 更多