【问题标题】:TableView on UIViewController with array information [closed]带有数组信息的 UIViewController 上的 TableView [关闭]
【发布时间】:2013-05-19 18:15:03
【问题描述】:

您好,我正在尝试在我的 uiviewcontroler 上使用 tableview 在我的 .h 中,我输入了以下代码:

@interface SecondViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *myTableView;
}
@property (nonatomic, retain) IBOutlet UITableView *myTableView;

在我的 .m 上:

我修改了我的代码,但现在它说我的 Response_array 未声明,并且在对象类型 uitableviewcell 上找不到 myTablevView

@synthesize myTableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_responseArray count];
}
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] init];
}
NSString *cellValue = [_responseArray objectAtIndex:indexPath.row];
[cell.textLabel setText:cellValue];
 return cell;
}

这是我的 Response_array

NSArray* Response_array = [json objectForKey:@"avenidas"];

【问题讨论】:

  • 错误出现在哪一行?
  • 在 -(UITableViewCell *)myTableView 行
  • -(UITableViewCell *)myTableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 将不会被调用,并且会导致错误,因为委托方法声明不正确。它应该是 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 另外,请检查以确保在方法主体之前和之后都有一个开始和结束括号 {}。
  • 谢谢,但现在我有其他错误
  • @darkjuso,没问题!我很高兴这有帮助。我在下面提供了答案。

标签: objective-c uitableview uiviewcontroller tableview


【解决方案1】:

问题:Response_array 未声明

在您的 @interface 文件中创建一个声明您的 NSArray 的属性

@property (retain, nonatomic) NSArray * responseArray;

在您的@implementation 文件@synthesize 属性中

@synthesize responseArray = _responseArray;

(可选)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

您可以使用 tableView 参数来访问 tableView 而不是 myTableView 属性。

例子:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

问题:在对象类型 uitableviewcell 上找不到 myTablevView

cell.myTableView = cellValue;

您正在尝试访问单元格中的 tableView? UITableViewCell 有一个名为 textLabel 的默认标签。

所以应该如下(除非你有自定义标签):

[cell.textLabel setText:cellValue];

【讨论】:

    【解决方案2】:

    看起来你有一个 nested 方法。

    换句话说,你有一个方法本质上是:

    - (IBAction)Avenida:(id)sender {
    
        -(UITableViewCell *)myTableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
    
        }
    
    }
    

    难怪您的代码无法编译。

    您需要从您的“Avenida”操作方法中提取您的“cellForRowAtIndexPath”方法out。这应该是两个不同的方法。

    【讨论】:

    • 我把 cellForRowAtIndexPath 拿出来仍然得到“Invalid argument type UITableViewCell to unary expression”错误
    • 我设法把它发布了,现在我修改了我原来的帖子,出现了一个新错误
    • 所以现在你有一个新问题?这将是发布一个单独的新问题的绝佳机会。 :-) 无论如何,我很高兴能够帮助您解决第一个问题。您的“Response_Array”声明在哪个函数中?您应该将其设为实例变量(即将其放入您的“@interface”.h 文件中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多