0

我已经UITableView嵌入到了UIViewController,我已经按照在表格视图中显示列表所需的所有步骤进行操作

我对以下方法有疑问,它看起来像被调用但它返回null而不是列表中的项目

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",[self.list objectAtIndex:indexPath.row]);
}

感谢大家的想法

亚历山德罗

4

1 回答 1

0

在代码下方,列表正确显示,因此设置了出口和协议方法(委托,数据源)。

方法 tableview:didSelectRowAtIndexPath: 它打印出 (null) 而不是列表中的正确元素

//
//  MainViewController.m
//  ProvaTable
//
//  Created by Alessandro on 12/6/12.
//  Copyright (c) 2012 Alessandro. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) NSArray *listOfObjects;

@end

@implementation MainViewController

@synthesize tableView = _tableView;
@synthesize listOfObjects = _listOfObjects;

-(void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    self.listOfObjects = [NSArray arrayWithObjects:@"Object 1",@"Object 2", nil];
}

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

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

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell Item";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    }
    // Set up the cell...
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    cell.textLabel.text = [self.listOfObjects objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",[self.listOfObjects objectAtIndex:indexPath.row]);
}

@结尾

于 2012-12-07T13:47:46.813 回答