iOS 7-TableView未正确显示值
对不起,我陷入这个问题已经有一段时间了,我不知道为什么。我有一个ViewController,它有两个TableView,我花了一些时间来管理这些功能,但现在运行得很好。最初,我有一些静态字符串作为表格单元格的显示,只是为了检查是否可以运行它。在设法做到这一点之后,我使用数组作为表内容的源。第一个表填充得很好,但是第二个表显示为空白 cellForRowAtIndexPath函数:iOS 7-TableView未正确显示值,ios,objective-c,uitableview,Ios,Objective C,Uitableview,对不起,我陷入这个问题已经有一段时间了,我不知道为什么。我有一个ViewController,它有两个TableView,我花了一些时间来管理这些功能,但现在运行得很好。最初,我有一些静态字符串作为表格单元格的显示,只是为了检查是否可以运行它。在设法做到这一点之后,我使用数组作为表内容的源。第一个表填充得很好,但是第二个表显示为空白 cellForRowAtIndexPath函数: - (UITableViewCell *)tableView:(UITableView *)tableView c
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *priceCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:PriceCellIdentifier];
UITableViewCell *discountCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:DiscountCellIdentifier];
if(tableView == self.priceTable){
[priceCell.textLabel setText:[NSString stringWithString:[priceArray objectAtIndex:indexPath.row]]];
return priceCell;
}
else /*if(tableView == self.discountTable)*/ {
[discountCell.textLabel setText:[NSString stringWithString:[discountArray objectAtIndex:indexPath.row]]];
return discountCell;
}
}
- (void)viewDidLoad{
[super viewDidLoad];
priceArray = [[NSMutableArray alloc] init];
discountArray = [[NSMutableArray alloc] init];
NSLog(@"received info %@", [self.selectedItem description]);
[self executeSQL];
}
这是相当令人困惑的,因为我除了更改表格单元格将显示的值的位置之外,什么都没有做。我还检查了我的discountArray
,它不是空的,我从这个函数中注销了值,我可以看到所说的值
编辑
以下是我的viewDidLoad函数:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *priceCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:PriceCellIdentifier];
UITableViewCell *discountCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:DiscountCellIdentifier];
if(tableView == self.priceTable){
[priceCell.textLabel setText:[NSString stringWithString:[priceArray objectAtIndex:indexPath.row]]];
return priceCell;
}
else /*if(tableView == self.discountTable)*/ {
[discountCell.textLabel setText:[NSString stringWithString:[discountArray objectAtIndex:indexPath.row]]];
return discountCell;
}
}
- (void)viewDidLoad{
[super viewDidLoad];
priceArray = [[NSMutableArray alloc] init];
discountArray = [[NSMutableArray alloc] init];
NSLog(@"received info %@", [self.selectedItem description]);
[self executeSQL];
}
executeSQL是初始化priceArray
和discountArray
- (void) executeSQL{
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSMutableString *sqlQuery = [NSMutableString stringWithString:@"SELECT ITEMNAME, BRAND, GROUPID, CLASS, UOM, AMT, AMT2, AMT3, DISC, DISC2, DISC3 FROM ItemList WHERE ITEMNAME = '"];
[sqlQuery appendString:selectedItem];
[sqlQuery appendString:@"'"];
// Query result
FMResultSet *resultsWithNameLocation = [database executeQuery:sqlQuery];
[priceArray removeAllObjects];
[discountArray removeAllObjects];
//we get the results here.
while([resultsWithNameLocation next]) {
//for the AMT, AMT2, and AMT3
if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"AMT"]] isEqualToString:@"(null)"]){
[priceArray addObject:@""];
}
else{
[priceArray addObject:[resultsWithNameLocation stringForColumn:@"AMT"]];
}
if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"AMT2"]] isEqualToString:@"(null)"]){
[priceArray addObject:@""];
}
else{
[priceArray addObject:[resultsWithNameLocation stringForColumn:@"AMT2"]];
}
if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"AMT3"]] isEqualToString:@"(null)"]){
[priceArray addObject:@""];
}
else{
[priceArray addObject:[resultsWithNameLocation stringForColumn:@"AMT3"]];
}
//end of priceArray
//for DISC, DISC2, and DISC3
if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"DISC"]] isEqualToString:@"(null)"]){
[discountArray addObject:@""];
}
else{
[discountArray addObject:[resultsWithNameLocation stringForColumn:@"DISC"]];
}
if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"DISC2"]] isEqualToString:@"(null)"]){
[discountArray addObject:@""];
}
else{
[discountArray addObject:[resultsWithNameLocation stringForColumn:@"DISC2"]];
}
if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"DISC3"]] isEqualToString:@"(null)"]){
[discountArray addObject:@""];
}
else{
[discountArray addObject:[resultsWithNameLocation stringForColumn:@"DISC3"]];
}
NSLog(@"DISC = %@", discountArray[0]);
NSLog(@"DISC2 = %@", discountArray[1]);
NSLog(@"DISC3 = %@", discountArray[2]);
}
[database close];
}
executeSQL
函数的log语句首先在cellforrowatinexpath
函数之前执行,该函数告诉我数组有内容。它们不仅被正确地显示出来
我很困惑,因为如果我替换[discountCell.textLabel setText:[NSString stringWithString:[discountArray objectAtIndex:indexPath.row]]代码>带有[discountCell.textLabel setText:[NSString stringWithFormat:@“示例文本”]代码>然后它就工作了
有人能帮我吗?谢谢。您可能没有为第二个UITableView设置代理。如果尚未执行此操作,请在viewDidLoad
中进行设置
仔细检查您的故事板,确保您的表视图是动态的。另外,您是否在tableView:numberOfRowsInSection:
我希望这有帮助 放置断点并查看它是否进入else部分??我在那里放置了一个日志行,是的,它进入else部分。另外,我知道它会转到else部分,因为当我使用静态字符串时,会填充第二个表。是否为这两个表都设置了委托?如何获取数据?是否有异步网络调用来获取它们?你能发布任何相关代码吗?我通过sql查询应用程序中存储的数据库来获取数据。我将编辑我的帖子。很抱歉听起来很傻,但是当我在故事板上设置代理时,它有什么不同吗?没有,真的没有什么不同。这是相当混乱的,我已经将TableView的数据源和代理出口重新连接到故事板中的视图控制器,但它仍然拒绝工作。如果我错过了为第二个TableView设置代理,我将无法正确显示静态字符串?请仔细检查您的故事板,确保您的TableView是动态的。另外,您是否在tableView:numberOfRowsInSection:中返回了正确的行数?我将numberOfRowsInSection
和NumberOfSection切换到tableView
。非常感谢。我不敢相信我在这个愚蠢的问题上浪费了1.5个小时。