Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios TableViewController-一个键值与其他键值的偏移_Ios_Uitableview_Nsindexpath - Fatal编程技术网

Ios TableViewController-一个键值与其他键值的偏移

Ios TableViewController-一个键值与其他键值的偏移,ios,uitableview,nsindexpath,Ios,Uitableview,Nsindexpath,我的TableViewController有问题。我正在向tableViewController发布3个值。scoreID、scoreDate和scoreValue。这一切似乎都正常工作,但我的问题是,scoreValue与scoreID和日期之间的偏移量为1。通过图片更容易显示: (“一个好的商业创意”应该具有“另一个好创意”的价值,等等) 现在,通过查看.sqlite数据库文件,我知道分数被1抵消了。所以我的问题是如何修复它 我的tableViewController.m如下所示: #im

我的TableViewController有问题。我正在向tableViewController发布3个值。scoreID、scoreDate和scoreValue。这一切似乎都正常工作,但我的问题是,scoreValue与scoreID和日期之间的偏移量为1。通过图片更容易显示:

(“一个好的商业创意”应该具有“另一个好创意”的价值,等等) 现在,通过查看.sqlite数据库文件,我知道分数被1抵消了。所以我的问题是如何修复它

我的tableViewController.m如下所示:

#import "CEWTableViewCell.h"

@interface CEWScoreTableViewController ()

@property (nonatomic) NSInteger countOfRows;
@property (nonatomic, strong) NSMutableArray *scoreIDsForTableCells;
@property (nonatomic, strong) NSArray *fetchedObjects;
@property (nonatomic, strong) NSDictionary *getScoreDataToDict;



@end

@implementation CEWScoreTableViewController{
    UITableView *tableView;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGFloat topLayoutGuide = self.topLayoutGuide.length;
    tableView.contentInset = UIEdgeInsetsMake(topLayoutGuide, 0, 0, 0);

    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;


    [self.tableView registerClass:[CEWTableViewCell class] forCellReuseIdentifier:@"CellID"];

    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];

    //INITIALISE APPDELEGATE AND CREATE INSTANCE. MAKE FETCH REQUEST AND POPULATE DATA TO NSDICTIONARY
    CEWAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    self.managedObjectContext = appDelegate.managedObjectContext;


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.resultType = NSDictionaryResultType;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ScoreData" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error = nil;
    self.fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchRequest == nil) {
        NSLog(@"an error occured fetching objects...!");
    }


    NSLog(@"objectForKey returns: %@", self.scoreIDFromEntity);






    NSUInteger countIDsForRows = [self.fetchedObjects count];
    self.countOfRows = countIDsForRows;

}

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


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

//This value is set to the number of @"scoreID"s returned
    return self.countOfRows;
}


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

    CEWTableViewCell *cell = (CEWTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CEWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    self.getIndexPath = indexPath.row;


    NSDictionary *getScoreIDs = [self.fetchedObjects objectAtIndex:[indexPath row]];

    NSDictionary *getDates = [self.fetchedObjects objectAtIndex:[indexPath row]];

    //This is just formatting the date to the format I want displayed
    NSDate *pullDates = [getDates valueForKey:@"scoreDate"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MMM-yyyy HH:MM"];
    NSString *formattedDate = [dateFormat stringFromDate:pullDates];

    //Here I fetch the @"scoreValue"s and convert them from a string, so I can add them up
    NSDictionary *getScoreValues = [self.fetchedObjects objectAtIndex:[indexPath row]];
    NSString *convertDictKeyToString  = [getScoreValues objectForKey:@"scoreValue"];

    NSArray *items = [convertDictKeyToString componentsSeparatedByString:@","];

        double total = 0.0;
        for (NSString *string in items)
        {
            total += [string floatValue];
        }
    cell.descriptionLabel.text = [getScoreIDs objectForKey:@"scoreID"];
    cell.dateLabel.text = formattedDate;
    cell.tableCellScoreLabel.text = [NSString stringWithFormat:@"%1.1f", total];

    return cell;
}
我希望这是足够的代码,否则让我知道,我会尝试提供更多

回答


苏。事实证明,我的标签是偏移的,因为CGRect的x,y,x,y值。。。。Ehrm…

根据我的经验,cellForRowAtIndexPath被调用的速度非常快,因此它没有太多的时间来执行计算,而且就我所了解的内容而言,格式化程序占用的资源比您想象的要多

我通常通过创建一个新的方法来解决这个问题,该方法从viewDidLoad或ViewWillDisplay调用,在这个方法中,我为我需要的数据创建一个数组或某种容器


那么cellforrowatinexpath只需通过索引调用数组。

soooooo。事实证明,我的标签是偏移的,因为CGRect的x,y,x,y值。。。。Ehrm。。。无论如何。。。我很感激你的回答,我确实重构了我的代码,使它更精简,性能更好。。。当我让代表投票时,我会回来投票支持你的答案!谢谢你花时间回答,很抱歉你太傻了!干杯,沙哈,没问题。我很高兴你无论如何都能找到这个问题