簡體   English   中英

將數據發送到detailViewController無法正常工作?

[英]Sending data to detailViewController not working?

我有一個UITableview,該數據庫中已填充了數據庫中的數據,我知道該如何將數據發送到另一個視圖(detailViewController),但是我無法使其正常工作,現在我正在通過- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {發送數據- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {方法。 我在下面有兩個類UITableview類和detail類。 我還有一個NSObject類,用於保存數據庫的對象。 請檢查一下,這是我的代碼:

UITableView類代碼。:

Header File

 #import <UIKit/UIKit.h>
 #import <sqlite3.h>

@interface ExerciseViewController : UITableViewController {
NSMutableArray *theauthors;
sqlite3 * db;

}
@property(nonatomic,retain) NSMutableArray *theauthors;

-(NSMutableArray *) authorList;

@end

實施文件

#import "ExerciseViewController.h"
#import "sqlColumns.h"
#import <sqlite3.h>
#import "UIColor+FlatUI.h"
#import "ExerciseDetailViewController.h"

@interface ExerciseViewController ()
@end
@implementation ExerciseViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"Abdominal";

[self authorList];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
}

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

return [self.theauthors count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"exerciseCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


sqlColumns *author = [self.theauthors objectAtIndex:indexPath.row];

UILabel *exerciseName = (UILabel *)[cell viewWithTag:101];
exerciseName.text = author.Name;

UILabel *equipment = (UILabel *)[cell viewWithTag:102];
equipment.text = author.Equipment;
NSString *string = author.Equipment;
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                           [NSCharacterSet whitespaceCharacterSet]];
if ([trimmedString isEqualToString:@"null"]) {
     equipment.text = @"No Equipment";
}

UILabel *difficulty = (UILabel *)[cell viewWithTag:103];
difficulty.text = author.Difficulty;

if ([difficulty.text isEqualToString:@"Easy"]) {
    difficulty.textColor = [UIColor emerlandColor];
}
if ([difficulty.text isEqualToString:@"Intermediate"]) {
    difficulty.textColor = [UIColor belizeHoleColor];
}
if ([difficulty.text isEqualToString:@"Hard"]) {
    difficulty.textColor = [UIColor alizarinColor];
}
if ([difficulty.text isEqualToString:@"Very Hard"]) {
    difficulty.textColor = [UIColor alizarinColor];
}

UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100];
cellImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",author.File]];

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor cloudsColor];
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];



return cell;
}

-(NSMutableArray *) authorList{
_theauthors = [[NSMutableArray alloc] initWithCapacity:10];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }
    const char *query = "SELECT * FROM strengthexercises WHERE primarymuscle LIKE '%abdominal%'";
    const char *sql = query;

    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            sqlColumns * author = [[sqlColumns alloc] init];
            author.Name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            author.Muscle = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
            author.Description = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
            author.File= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)];
            author.Sets= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
            author.Reps= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 6)];
            author.Equipment= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 7)];
            author.PrimaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 8)];
            author.SecondaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 9)];
            author.Difficulty= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 10)];

            [_theauthors addObject:author];
        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);

    return _theauthors;
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"detail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    sqlColumns *author = [self.theauthors objectAtIndex:indexPath.row];
    ExerciseDetailViewController *destViewController = segue.destinationViewController;
    destViewController.exerciseImage.image = [UIImage imageNamed:author.File];
    destViewController.descriptionLabel.text = author.Description;

        }

 }


 @end

現在,DetailViewController

頭文件:

#import <UIKit/UIKit.h>
#import "sqlColumns.h"

@interface ExerciseDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *exerciseImage;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
@property (nonatomic, strong) sqlColumns *author;

@end

實施文件

#import "ExerciseDetailViewController.h"
#import "ExerciseViewController.h"

@interface ExerciseDetailViewController ()

@end

@implementation ExerciseDetailViewController
@synthesize exerciseImage,descriptionLabel,author;

- (void)viewDidLoad
{
[super viewDidLoad];

descriptionLabel.text = author.Description;
NSLog(@"%@",author.Description);
}


@end

必須是一個小錯誤,任何幫助將不勝感激!

我認為問題在於您的門店數量為零。 您只能在調用viewDidLoad之后設置文本和圖像。

嘗試將您的信息保存在其他屬性中,並在調用viewDidLoad之后,將此信息分配給標簽和圖像視圖。

在准備進行測試時:

destViewController.image = [UIImage imageNamed:author.File];
destViewController.text = author.Description;

在標題中添加以下屬性:

@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSString *text;

然后在您的viewDidLoad中:

exerciseImage.image = self.image;
descriptionLabel.text = self.text;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM