【问题标题】:How to move to the next row of tableView by Button如何通过Button移动到tableView的下一行
【发布时间】:2013-06-28 19:42:59
【问题描述】:

这是我的问题:

通过按下 DetailViewController 上的按钮,我想接收下一行的数据。

这是我目前尝试过的:

-(IBAction)next:(id)sender {
question.text = [questions objectAtIndex:indexPath.row+1];
}

但现在它需要存储在第 1 行中的数据。但我想从下一行检索数据。

question是我希望在其中显示文本的标签的名称。
questions是我的 NSMutableArray 的名称,用于存储我的数据。

这是完整的代码:

//
//  NINDetailViewController.m
//  NIN
//
//  Created by Hegg Roger on 27.06.13.
//  Copyright (c) 2013 Hegg. All rights reserved.
//

#import "NINDetailViewController.h"
#import "NIN.h"

@interface NINDetailViewController ()

@end

@implementation NINDetailViewController

@synthesize transfer;
@synthesize content;
@synthesize questions;
@synthesize questions1;
@synthesize answers;
@synthesize question;
@synthesize questionTableView;
@synthesize indexPath;


/*
-(IBAction)next:(id)sender {
question.text = [questions objectAtIndex:indexPath.row+1];
}      


-(IBAction)prev:(id)sender {

}
*/

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

- (void)viewDidLoad
{

questions = [[NSMutableArray alloc]initWithObjects:
              @"1. Wie lautet die richtige Reihenfolge beim Arbeiten an elektrischen
Installationen (Arbeitssicherheit)?",
             @"2. Was versteht man unter dem Begriff UVG?",
             @"3. Was ist die wichtigste Bestimmung des UVGs?",nil];



questions1 = [[NSMutableArray alloc]initWithObjects:
             @"1. Was bedeuten die Abkürzungen NIV / NIN / StV?",nil];


[super viewDidLoad];




content.text = transfer;
 //  question.text = [questions objectAtIndex:indexPath.row];


}



- (NSInteger)tableView : (UITableView *)tableView numberOfRowsInSection:(NSInteger)section       
{


return [questions count];
 }

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    
 *)indexPath
{
static NSString *simpleTableIdentifier = @"questionCell";

UITableViewCell *cell = [questionTableView 
dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:simpleTableIdentifier];
}

if ([content.text isEqualToString:@"Unfallverhütung"]) {
    question.text = [questions objectAtIndex:indexPath.row];
}
else
    if ([content.text isEqualToString:@"Grundlagen / Begriffe / Kennzeichnungen"]) {
        question.text = [questions1 objectAtIndex:indexPath.row];
    }



return cell;
}



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

@end

@TBlue:您的建议出现了这个错误:

【问题讨论】:

    标签: objective-c row tableview nsindexpath


    【解决方案1】:

    除非 indexPath 是您在某处定义的全局变量,否则您会遇到一个非常明显的问题。也就是说,indexPath 不在您在上面发布的方法中访问它的范围内...除非您未能在该方法中发布整个代码...

    【讨论】:

      【解决方案2】:

      您的阵列放在哪里?变量呢?

      一些代码在这里真的很有帮助。听起来您的 indexPath 没有在您使用它的地方定义,这意味着您可能会添加 nil + 1,这可能是您的第 1 行问题的根源。

      发布一些代码,以便我们提供更多帮助!

      【讨论】:

        【解决方案3】:

        试试类似的东西

        @interface ViewController () {
            NSInteger selectedindex;
        }
        
        - (void)viewDidLoad {
            [super viewDidLoad];
            selectedindex = -1;
        }
        
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
            selectedindex = indexPath.row;
        }
        
        - (IBAction)next:(id)sender {
            if (questions.count == 0 || selectedindex < 0) {
                return;
            }
            question.text = [questions objectAtIndex:selectedindex + 1];
        }
        

        【讨论】:

        • 感谢您的努力,但不幸的是我得到了错误:(见图片)
        • 我建议你仔细看看我发布的前三行代码。
        • 再次感谢,但我发布的完整代码 dosnt contain your solution. Iv 尝试过,得到错误,截取屏幕截图并再次删除。我忘了提到我正在使用 Storyboard 和 ARC。
        • 您收到错误消息,因为您没有将 selectedindex 实现为类范围的变量。你好像不听。这就是我的全部。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-30
        相关资源
        最近更新 更多