gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:08:03 25 4
gpt4 key购买 nike

我的 TableViewController 有问题。我将 3 个值发布到 tableViewController。 scoreID、scoreDate 和 scoreValue。一切似乎都正常工作,但我的问题是,scoreValue 相对于 scoreID 和 Date 偏移了 1。用图片更容易展示:

As you can see the top cell isn't populated with the score.

(“一个好的商业创意”应该具有“另一个好创意”的值(value)等等)现在,通过查看 .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 值,我的标签被偏移了……嗯……

最佳答案

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

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

然后 cellForRowAtIndexPath 只需要通过索引调用数组。

关于ios - TableViewController - 一个键值与其他键值的偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22547931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com