Iphone 奇数UITableView行为
请看一下图片 我有一个自定义单元格和2个部分的UITableView。一旦UITableView填充了足够多的单元格,以至于它们都无法显示在屏幕上,最后一部分中的最后一个单元格将显示在屏幕的最顶部。该单元格甚至不应该在UITableView中可见,因为它是最后一个单元格,并且它完全显示在屏幕顶部的UITableView之外。我甚至不知道从哪里开始查找我的代码来解决这个问题。我只创建了几个UITableView,所以我肯定不是这个bug所有潜在原因的专家。表是通过IB设置的,初始化代码在VIEW中,尽管没有太多的设置代码。我目前正在阅读UITableView的scrollView以及contentInset,以了解是否有可能对这种行为进行解释。有人遇到过这样的错误吗?我觉得它足够独特,可能不需要梳理所有代码来确定。不管怎样,我将在下面发布相关代码。谢谢你的帮助Iphone 奇数UITableView行为,iphone,ios,objective-c,Iphone,Ios,Objective C,请看一下图片 我有一个自定义单元格和2个部分的UITableView。一旦UITableView填充了足够多的单元格,以至于它们都无法显示在屏幕上,最后一部分中的最后一个单元格将显示在屏幕的最顶部。该单元格甚至不应该在UITableView中可见,因为它是最后一个单元格,并且它完全显示在屏幕顶部的UITableView之外。我甚至不知道从哪里开始查找我的代码来解决这个问题。我只创建了几个UITableView,所以我肯定不是这个bug所有潜在原因的专家。表是通过IB设置的,初始化代码在VIEW中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *uniqueIdentifierForPlayerCell = @"customPlayerCell";
//Initialize PlayerTableViewCell and set it's properties
PlayerTableViewCell *playerCell = nil;
playerCell = (PlayerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifierForPlayerCell];
if (playerCell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PlayerTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[PlayerTableViewCell class]]) {
playerCell = (PlayerTableViewCell *)currentObject;
break;
}
}
}
playerCell.textLabel.opaque = NO;
playerCell.textLabel.textColor = self.textColor;
playerCell.textLabel.font = [UIFont systemFontOfSize:18.0];
playerCell.textLabel.backgroundColor = [UIColor clearColor];
playerCell.selectionStyle = UITableViewCellSelectionStyleNone;
playerCell.accessoryButton.hidden = YES;
playerCell.reorderButton.hidden = YES;
NSString *uniqueIdentifierForAlleyCell = @"customAlleyCell";
//Initialize AlleyTableViewcell and set it's properties
AlleyTableViewCell *alleyCell = nil;
alleyCell = (AlleyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifierForAlleyCell];
if (alleyCell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AlleyTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[AlleyTableViewCell class]]) {
alleyCell = (AlleyTableViewCell *)currentObject;
break;
}
}
}
alleyCell.textLabel.opaque = NO;
alleyCell.textLabel.textColor = self.textColor;
alleyCell.textLabel.font = [UIFont systemFontOfSize:18.0];
alleyCell.textLabel.backgroundColor = [UIColor clearColor];
alleyCell.selectionStyle = UITableViewCellSelectionStyleNone;
alleyCell.accessoryButton.hidden = YES;
alleyCell.deleteButton.hidden = YES;
//Alternate background colors for each section where section 0 is Alley section and section 1 is Player section. Stores the RGB values of the specific background color to the specific cell for future use
if(indexPath.section == 0 && (indexPath.row %2 == 0)) {
alleyCell.contentView.backgroundColor = self.backgroundColor;
alleyCell.isRed = 95.0/255.0;
alleyCell.isGreen = 94.0/255.0;
alleyCell.isBlue = 94.0/255.0;
self.alleyField.backgroundColor = self.backgroundColor;
}
else if(indexPath.section == 0 && (indexPath.row %2 == 1)){
alleyCell.contentView.backgroundColor = self.alternateColor;
alleyCell.isRed = 92.0/255.0;
alleyCell.isGreen = 92.0/255.0;
alleyCell.isBlue = 92.0/255.0;
self.alleyField.backgroundColor = self.alternateColor;
}
else if(indexPath.section == 1 && (indexPath.row %2 == 0)) {
playerCell.contentView.backgroundColor = self.backgroundColor;
playerCell.isRed = 95.0/255.0;
playerCell.isGreen = 94.0/255.0;
playerCell.isBlue = 94.0/255.0;
self.playerField.backgroundColor = self.backgroundColor;
}
else if(indexPath.section == 1 && (indexPath.row %2 == 1)) {
playerCell.contentView.backgroundColor = self.alternateColor;
playerCell.isRed = 92.0/255.0;
playerCell.isGreen = 92.0/255.0;
playerCell.isBlue = 92.0/255.0;
self.playerField.backgroundColor = self.alternateColor;
}
//If Alley array is empty, add alley TextField and return cell
if(indexPath.section == 0) {
if([self.allAlleys count] == 0) {
[alleyCell addSubview:self.alleyField];
self.alleyField.text = nil;
return alleyCell;
}
//Else set the alley name to the nameLabel text and return cell
else {
if(indexPath.row < [self.allAlleys count]) {
alleyCell.textLabel.text = [self.allAlleys objectAtIndex:indexPath.row];
if([alleyCell.textLabel.text isEqualToString: self.selectedAlley]) {
alleyCell.accessoryButton.hidden = NO;
}
else {
alleyCell.accessoryButton.hidden = YES;
}
return alleyCell;
}
//Add alley TextField to the last cell of the UITableView and return cell
else {
[alleyCell addSubview:self.alleyField];
self.alleyField.text = nil;
return alleyCell;
}
}
}
//If Player array is empty, add Player TextField and return cell
else if(indexPath.section == 1) {
if([self.bowlrNames count] == 0) {
[playerCell addSubview:self.playerField];
self.playerField.text = nil;
return playerCell;
}
//Else set the Player name to the nameLabel text and return cell
else {
if(indexPath.row < [self.bowlrNames count]) {
playerCell.textLabel.text = [self.bowlrNames objectAtIndex:indexPath.row];
for(int i = 0; i < [self.selectedBowlrs count]; i++) {
if([playerCell.textLabel.text isEqualToString: [self.selectedBowlrs objectAtIndex:i]]) {
playerCell.accessoryButton.hidden = NO;
playerCell.reorderButton.hidden = NO;
}
}
return playerCell;
}
//Add Player textField to the last cell of the UITableView and return cell
else {
[playerCell addSubview:self.playerField];
self.playerField.text = nil;
return playerCell;
}
}
}
}
//Actions for selecting a row in the UITable
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//If it is in the Alley section
if(indexPath.section == 0) {
//If the selected cell is the same as the last selected cell
if(self.lastIndexPathForAlleyCells && indexPath.row == self.lastIndexPathForAlleyCells.row) {
[tableView beginUpdates];
self.lastIndexPathForAlleyCells = nil;
AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
previousCell.selected = NO;
self.selectedAlley = nil;
previousCell.accessoryButton.hidden = YES;
[tableView endUpdates];
return;
}
//Else the selected cell is not the last selected cell
AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[tableView cellForRowAtIndexPath:self.lastIndexPathForAlleyCells];
previousCell.selected = NO;
previousCell.accessoryButton.hidden = YES;
AlleyTableViewCell *cell = (AlleyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.selected = YES;
cell.accessoryButton.hidden = NO;
self.selectedAlley = cell.textLabel.text;
self.lastIndexPathForAlleyCells = indexPath;
[self.tableView reloadData];
}
else if(indexPath.section == 1) {
PlayerTableViewCell *cell = (PlayerTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//If player is not already selected
if([self playerIsSelected:cell.textLabel.text] == NO) {
//Set selected to YES and reveal buttons
cell.selected = YES;
cell.accessoryButton.hidden = NO;
cell.reorderButton.hidden = NO;
//If 8 Players are currently selected, UIAlertView informs user they have reached the limit
if([self.selectedBowlrs count] == 8) {
NSString *message = @"You can only select up to 8 players";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Max Players Selected" message:message delegate:self cancelButtonTitle:[self okButtonTitle] otherButtonTitles:nil, nil];
alertView.tag = TAG_FULLGAME;
[alertView show];
}
else {
//Insert name into selectedBowlrs array
[self.selectedBowlrs insertObject:cell.textLabel.text atIndex:0];
//Reorder bowlrNames array
NSString *bowlrName = [self.bowlrNames objectAtIndex:indexPath.row];
[self.bowlrNames removeObjectAtIndex:indexPath.row];
[self.bowlrNames insertObject:bowlrName atIndex:0];
//Move selected row to top of section
[self moveIndexPathToTop:indexPath];
}
}
//Else the player is already selected
else {
//Set selected to NO and hide buttons
cell.selected = NO;
cell.accessoryButton.hidden = YES;
cell.reorderButton.hidden = YES;
[self.selectedBowlrs removeObject:cell.textLabel.text];
[self.bowlrNames removeObject:cell.textLabel.text];
[self.bowlrNames insertObject:cell.textLabel.text atIndex:[self.selectedBowlrs count]];
[self moveIndexPathToMiddle:indexPath];
for(int i = 0; i < [self.selectedBowlrs count]; i++) {
NSLog(@"%@", [self.selectedBowlrs objectAtIndex:i]);
}
}
}
[self performSelector:@selector(reloadData) withObject:nil afterDelay:.25];
}
//Returns the number of rows for each section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
//For Alley section
case 0:
//If the array is empty, return 1 row for the Alley Textfield
if([self.allAlleys count] == 0)
return 1;
//Else return the number of elements in the array + 1 for the Alley Textfield
else
return [self.allAlleys count] + 1;
break;
//For Player section
case 1:{
//If the array is empty, return 1 row for the Player Textfield
if([self.bowlrNames count] == 0)
return 1;
//Else return the number of elements in the array + 1 for the Player Textfield
else
return [self.bowlrNames count] + 1;
break;
}
default:
break;
}
return 0;
}
//Set up appearance for section headers
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.tableView.bounds.size.width, 25.0)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
CGFloat isRed = 84.0/255.0;
CGFloat isGreen = 84.0/255.0;
CGFloat isBlue = 84.0/255.0;
self.headerColor = [[UIColor alloc]initWithRed:isRed green:isGreen blue:isBlue alpha:0.5];
headerLabel.backgroundColor = self.headerColor;
headerLabel.opaque = NO;
headerLabel.textColor = self.textColor;
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:18.0];
headerLabel.frame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, 25.0);
if (section == 0) {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = @"Alley";
[headerView addSubview:headerLabel];
}
else {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = @"Bowlr";
[headerView addSubview:headerLabel];
}
return headerView;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//Used to delete row of UITable
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
self.indexPathForEditing = indexPath;
if(editingStyle == UITableViewCellEditingStyleDelete) {
//For Alley section
if(indexPath.section == 0) {
//Set up UIAlertView
NSString *message = @"Are you sure?";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Delete Alley?" message:message delegate:self cancelButtonTitle:[self cancelButtonTitle] otherButtonTitles:[self deleteButtonTitle], nil];
alertView.tag = TAG_Alley;
[alertView show];
}
//For Player section
else if(indexPath.section == 1) {
//Set up UIAlertView
NSString *message = @"All information for this Bowlr will be deleted";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Delete Bowlr?" message:message delegate:self cancelButtonTitle:[self cancelButtonTitle] otherButtonTitles:[self deleteButtonTitle], nil];
alertView.tag = TAG_Player;
[alertView show];
}
}
[self performSelector:@selector(reloadData) withObject:nil afterDelay:.25];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString*uniqueIdentifierForPlayerCell=@“customPlayerCell”;
//初始化PlayerTableViewCell并设置其属性
PlayerTableViewCell*playerCell=nil;
playerCell=(PlayerTableViewCell*)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifierForPlayerCell];
如果(playerCell==nil){
NSArray*topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@“PlayerTableViewCell”所有者:nil选项:nil];
用于(topLevelObjects中的id currentObject){
if([currentObject isKindOfClass:[PlayerTableViewCell类]]){
playerCell=(PlayerTableViewCell*)当前对象;
打破
}
}
}
playerCell.textLabel.不透明=否;
playerCell.textLabel.textColor=self.textColor;
playerCell.textLabel.font=[UIFont systemFontOfSize:18.0];
playerCell.textLabel.backgroundColor=[UIColor clearColor];
playerCell.selectionStyle=UITableViewCellSelectionStyleNone;
playerCell.accessoryButton.hidden=是;
playerCell.reorderButton.hidden=是;
NSString*uniqueIdentifierForAlleyCell=@“customAlleyCell”;
//初始化AlleyTableViewcell并设置其属性
AlleyTableViewCell*alleyCell=nil;
alleyCell=(AlleyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifierForAlleyCell];
如果(alleyCell==nil){
NSArray*topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@“AlleyTableViewCell”所有者:nil选项:nil];
用于(topLevelObjects中的id currentObject){
if([currentObject isKindOfClass:[AlleyTableViewCell类]]){
alleyCell=(AlleyTableViewCell*)currentObject;
打破
}
}
}
alleyCell.textlab.不透明=否;
alleyCell.textlab.textColor=self.textColor;
alleyCell.textlab.font=[UIFont systemFontOfSize:18.0];
alleyCell.textLabel.backgroundColor=[UIColor clearColor];
alleyCell.selectionStyle=UITableViewCellSelectionStyleNone;
alleyCell.accessoryButton.hidden=是;
alleyCell.deleteButton.hidden=是;
//每个部分的备用背景色,其中第0部分为球道部分,第1部分为球员部分。将特定背景色的RGB值存储到特定单元格以备将来使用
if(indexPath.section==0&(indexPath.row%2==0)){
alleyCell.contentView.backgroundColor=self.backgroundColor;
alleyCell.isRed=95.0/255.0;
alleyCell.isGreen=94.0/255.0;
alleyCell.isBlue=94.0/255.0;
self.alleyField.backgroundColor=self.backgroundColor;
}
else if(indexPath.section==0&(indexPath.row%2==1)){
alleyCell.contentView.backgroundColor=self.alternateColor;
alleyCell.isRed=92.0/255.0;
alleyCell.isGreen=92.0/255.0;
alleyCell.isBlue=92.0/255.0;
self.alleyField.backgroundColor=self.alternateColor;
}
else if(indexPath.section==1&(indexPath.row%2==0)){
playerCell.contentView.backgroundColor=self.backgroundColor;
playerCell.isRed=95.0/255.0;
playerCell.isGreen=94.0/255.0;
playerCell.isBlue=94.0/255.0;
self.playerField.backgroundColor=self.backgroundColor;
}
else if(indexPath.section==1&(indexPath.row%2==1)){
playerCell.contentView.backgroundColor=self.alternateColor;
playerCell.isRed=92.0/255.0;
playerCell.isGreen=92.0/255.0;
playerCell.isBlue=92.0/255.0;
self.playerField.backgroundColor=self.alternateColor;
}
//如果Alley数组为空,则添加Alley文本字段和返回单元格
if(indexPath.section==0){
如果([self.allAlleys count]==0){
[alleyCell addSubview:self.alleyField];
self.alleyField.text=nil;
返回alleyCell;
}
//否则,将通道名称设置为nameLabel文本并返回单元格
否则{
if(indexath.row<[self.allAlleys count]){
alleyCell.textlab.text=[self.allAlleys objectAtIndex:indexPath.row];
if([alleyCell.textLabel.text IsequalString:self.selectedAlley]){
alleyCell.accessoryButton.hidden=否;
}
否则{
alleyCell.accessoryButton.hidden=是;
}
返回alleyCell;
}
//将alley TextField添加到UITableView的最后一个单元格并返回单元格
否则{
[alleyCell addSubview:self.alleyField];
self.alleyField.text=nil;
返回alleyCell;
}
}
}
//如果播放器数组为空,则添加播放器文本字段和返回单元格
else if(indexPath.section==1){
如果([self.bowlNames count]==0){
[playerCell addSubview:self.playerField];
self.playerField.text=nil;
返回playerCell;
}
//否则将播放器名称设置为名称标签文本a