0

我在 xcode 4.6 中遇到问题。

MainStoryboard 包含一个 Button 和 TableView。显然,当我运行应用程序并单击按钮时,表格视图仅显示第一列和最后一列数据。

我正在使用 sqlite3,我在其中创建了一个数据库和表,该表当前具有一行由 14 列组成的数据。第一列是您信息的主键。

下面是我的 UITableViewCell 代码 * 操作按钮。

任何见解将不胜感激。

谢谢。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

DefenseStats *aDefenseStats = [arrayOfDefenseStats objectAtIndex:indexPath.row];

cell.textLabel.text = aDefenseStats.defense_team_name_mp;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_games_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",aDefenseStats.defense_points_per_game_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",aDefenseStats.defense_yards_per_game_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",aDefenseStats.defense_rushing_yards_per_game_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",aDefenseStats.defense_passing_yards_per_game_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_interception_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_interception_touchdown_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_forced_fumble_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_defensive_touchdown_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_tackle_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_pass_deflection_mp];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_sack_mp];

return cell;

}

  • (IBAction)displayDefenseStatsButton:(id)sender {

    sqlite3_stmt *语句;

    if (sqlite3_open([dbPathString UTF8String], &americanfootballDB)==SQLITE_OK) { [arrayOfDefenseStats removeAllObjects];

    NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM DEFENSE_TEAM_STATS"];
    const char* query_sql = [querySql UTF8String];
    
    if (sqlite3_prepare(americanfootballDB, query_sql, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW)
        {
    
            NSString *defense_team_name_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
            NSString *defense_games_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
            NSString *defense_points_per_game_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
            NSString *defense_yards_per_game_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)];
            NSString *defense_rushing_yards_per_game_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 5)];
            NSString *defense_passing_yards_per_game_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 6)];
            NSString *defense_interception_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 7)];
            NSString *defense_interception_touchdown_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 8)];
            NSString *defense_forced_fumble_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 9)];
            NSString *defense_defensive_touchdown_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 10)];
            NSString *defense_tackle_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 11)];
            NSString *defense_pass_deflection_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 12)];
            NSString *defense_sack_string = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 13)];
    
            DefenseStats *defensestats =[[DefenseStats alloc]init];
    
            [defensestats setDefense_team_name_mp:defense_team_name_string];
            [defensestats setDefense_games_mp:[defense_games_string intValue]];
            [defensestats setDefense_points_per_game_mp:[defense_points_per_game_string floatValue]];
            [defensestats setDefense_yards_per_game_mp:[defense_yards_per_game_string floatValue]];
            [defensestats setDefense_rushing_yards_per_game_mp:[defense_rushing_yards_per_game_string floatValue]];
            [defensestats setDefense_passing_yards_per_game_mp:[defense_passing_yards_per_game_string floatValue]];
            [defensestats setDefense_interception_mp:[defense_interception_string intValue]];
            [defensestats setDefense_interception_touchdown_mp:[defense_interception_touchdown_string intValue]];
            [defensestats setDefense_forced_fumble_mp:[defense_forced_fumble_string intValue]];
            [defensestats setDefense_defensive_touchdown_mp:[defense_defensive_touchdown_string intValue]];
            [defensestats setDefense_tackle_mp:[defense_tackle_string intValue]];
            [defensestats setDefense_pass_deflection_mp:[defense_pass_deflection_string intValue]];
            [defensestats setDefense_sack_mp:[defense_sack_string intValue]];
    
            [arrayOfDefenseStats addObject:defensestats];
        }
    }
    

    } [[self myTableView]reloadData]; } */

4

1 回答 1

0

由于您cell.detailTextLabel.text一遍又一遍地分配到同一个位置 ( ),因此只有最后一个会计算在内。这就是为什么您只看到最后一列的原因。

cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",aDefenseStats.defense_sack_mp];

我认为您真正想要的是每列的一个单元格。您将通过以下方式获得:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return 14;
}

然后更新您的tableView:cellForRowAtIndexPath:方法以选择适当的列值,可能使用 14-caseswitch语句并将值分配给cell.textLabel.text

NSString *defenseStatValue;
switch (indexPath.row)
{
  case (0):
    defenseStatValue = aDefenseStats.defense_team_name_mp;
    break;
  case (1):
    defenseStatValue = [NSString stringWithFormat:@"%d",aDefenseStats.defense_games_mp];
    break;
  /* Add a case for each column index possible. */
}
cell.textLabel.text = defenseStatValue;

另外,不要将任何内容分配给cell.detailTextLabel.text.

于 2013-06-10T21:54:39.440 回答