1

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

视图控制器.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

视图控制器.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

第二视图控制器.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

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

4

5 回答 5

1

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

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

于 2012-10-29T05:15:02.723 回答
1
[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];

于 2012-10-29T05:16:49.013 回答
1

在 SecondViewController 中调用-(void) printRowNumber:(int)numviewDidLoad 中的方法并[secondViewController printRowNumber:indexPath.row+1];rowDidSelectfirstViewController 中移除。

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

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

于 2012-10-29T05:24:59.303 回答
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];

}
于 2012-10-29T05:26:55.653 回答
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 presentViewController:secondViewController animated:YES completion:^{
        [secondViewController printRowNumber:indexPath.row+1];
    }];
}

在您呈现控制器后,将创建 SecondViewController 中的视图。因此,如果您在展示控制器之前调用 -printRowNumber: ,您的标签仍未创建。

于 2012-10-29T05:30:31.953 回答