我正在开发一款主要使用1个视图控制器的应用。当用户按下按钮时,它会填充一个数组并使用该数组使用[myTable reloadData]填充隐藏的表视图。
然后他可以按一个按钮,表就会消失,而另一个按钮则应该再次执行相同的操作,使用相同的功能填充相同的数组并重新加载数据。
但是当我这样做时,单元格的大小会增加,它会强制我的表子视图增加大小,因此使用滚动来显示所有数据,而第一次它完全适合表子视图。 / p>
我可能在我的应用的故事板选项中出错了,但是我的代表团的方法以防万一:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"playerCell" forIndexPath:indexPath];
cell.textLabel.text = [NSMutableString stringWithFormat:@"Player %ld", (((Player *)[topPlayersArray objectAtIndex:indexPath.row]).playerID + 1)];
//display the player's poiunts
cell.detailTextLabel.text = [NSMutableString stringWithFormat:@"Points: %ld", (((Player *)[topPlayersArray objectAtIndex:indexPath.row]).playerScore)];
return cell;
}
//custom font and color for the cell
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//set up the cell's font
cell.textLabel.font = [UIFont fontWithName:@"Aka-AcidGR-ScrachThis" size:34.0];
cell.detailTextLabel.font = [UIFont fontWithName:@"Aka-AcidGR-ScrachThis" size:24.0];
cell.detailTextLabel.textColor = [UIColor whiteColor];
//set the color of the label
if(indexPath.row == 0)
{
//set color to gold for first player
cell.textLabel.textColor = [UIColor colorWithRed:252.0/255.0 green:194.0/255.0 blue:0 alpha:1.0];
}
else if(indexPath.row == 1)
{
//set color to silver for second player
cell.textLabel.textColor = [UIColor colorWithRed:191.0/255.0 green:191.0/255.0 blue:191.0 alpha:1.0];
}
else
{
//set color to bronze for 3rd player
cell.textLabel.textColor = [UIColor colorWithRed:0.647 green:0.443 blue:0.392 alpha:1.0];
}
}
//return th e number of rows, should be 0 when the game starts and 2 or 3 depending on the number of players (2 for 2, 3 for 3 or more)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [topPlayersArray count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//create header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//create the header subview
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 48)];
//create the section title label
UILabel *labelHeader = [[UILabel alloc] initWithFrame:CGRectMake (0,0,320,44)];
//set it to the scrachThis font and white color and center align the text
labelHeader.font = [UIFont fontWithName:@"Aka-AcidGR-ScrachThis" size:34.0];
labelHeader.textColor = [UIColor whiteColor];
labelHeader.textAlignment = NSTextAlignmentCenter;
//set the text and return the headerView
if(thisGame.gameHasEnded)
{
labelHeader.text = @"Winners:";
}
else
{
labelHeader.text = @"Leading Players:";
}
[headerView addSubview: labelHeader];
return headerView;
}
是的,该表应该最多有3个单元
答案 0 :(得分:2)
只需使用:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
并返回适当的高度。