TrafficDetailViewController

TrafficDetailViewController

我正在使用表格视图来显示数据数组,我想传递该值以在局部视图控制器中更新Labels和image视图。我实际上创建了一个自定义单元格,并将其加载到“表格”视图中,并在选择一行时推送了带有标签和ImageView的新的自定义视图控制器。我想使用所选单元格的值显示在自定义视图控制器的标签中。代码可以编译,但是唯一的是细节视图中的“标签”和“图像”视图不会更新。我只是不知道我要去哪里错了。。。以下是我的应用中的代码…

code in table view…

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tableData count];
 }
   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


if (cell == nil)
{


    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

}

cell.LocationLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.TimeLabel.text = [time objectAtIndex:indexPath.row];
return cell;
   }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];

trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];
 //transfering control to detail view
[[self navigationController] pushViewController:trailsController animated:YES];
 }

TrafficTableViewViewController.m
   @synthesize trafficImage = _trafficImage;
   @synthesize LocationLable = _LocationLable;

在上面的代码中TrafficDetailViewController是我创建的自定义视图控制器,它包含标签和UIImage。请让我知道我要去哪里了...

最佳答案

尝试使用此代码,可能会解决您的问题。问题是您在按下控制器后设置了属性,这是错误的。试试下面的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
    trailsController.trafficImage = [thumbnails objectAtIndex:indexPath.row];
    trailsController.LocationLable = [tableData objectAtIndex:indexPath.row];
    //transfering control to detail view
    [[self navigationController] pushViewController:trailsController animated:YES];
 }

编辑

.h文件
在.m文件中

如果尚未添加,请添加合成器。
@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;

当您尝试推送TrafficDetailViewController时,还要编辑一件事

然后将图像分配给trafficImage.image而不是trafficImage,并将字符串分配给LocationLabel.text,因为trafficImage是一个ImageView而不是Image,而LocationLable是一个标签而不是string.So用以下代码替换行
trailsController.trafficImage.image = [thumbnails objectAtIndex:indexPath.row];
trailsController.LocationLable.text = [tableData objectAtIndex:indexPath.row];

POST EDIT

在.h文件中
@interface TrafficDetailViewViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *trafficImage;
@property (strong, nonatomic) IBOutlet UILabel *LocationLable;
//Add these two more properties in TrafficDetailViewController
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *locationString;

@end

在.m文件中:
@synthesize trafficImage = _trafficImage;
@synthesize LocationLable = _LocationLable;
@synthesize image = _image;
@synthesize locationString = _locationString;

在viewDidLoad方法中:
- (void) viewDidLoad
{
    self.trafficImage.image = _image;
    self.LocationLabel.text = _locationString;
}

然后在编写了TrafficDetailviewController的推送代码的Previous viewController中,用您的代码替换以下代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TrafficDetailViewViewController *trailsController = [[TrafficDetailViewViewController alloc] init];
    trailsController.image = [thumbnails objectAtIndex:indexPath.row];
    trailsController.locationString = [tableData objectAtIndex:indexPath.row];
    //transfering control to detail view
    [[self navigationController] pushViewController:trailsController animated:YES];
 }

这背后的原因是您的ViewController在推送之前未加载,而我们正在尝试设置imageView.image和Lable.text,我认为这是导致问题的原因。请尝试一下,让我知道它现在可以工作了吗?

享受编码!

10-02 15:59