我是 xcode5 的新手。我曾经在我的应用程序中对 tableview 进行分组。它在 iOS6.1 中工作正常,但在 iOS7 中不显示数据。它只是显示“标题”部分。那是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[atableView reloadData];
self.notification = [[UISwitch alloc] initWithFrame:CGRectZero];
LoginSetting = [[NSArray alloc] initWithObjects:@"Company ID",@"Username",nil];
[LoginSetting retain];
mic = [[NSArray alloc] initWithObjects:@"Notification",@"App Version",@"Server",nil];
[mic retain];
detailText=[[NSArray alloc]initWithObjects:@"kaisquare",@"admin",nil];
[detailText retain];
logout=[[NSArray alloc]initWithObjects:@"",nil];
self.title = @"Setting";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return [LoginSetting count];
else if(section==1)
return [mic count];
else
return [logout count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
return @"Login Setting";
else if(section==1)
return @"Mics";
else
return @"";
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
if (indexPath.row == 0)
{
}
else if (indexPath.row == 1)
{
}
else
{
}
}
else if(indexPath.section==1)
{
if (indexPath.row == 0)
{
}
else if (indexPath.row == 1)
{
}
else if (indexPath.row == 2)
{
alertView = [[UIAlertView alloc] initWithTitle:@"Server" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Okay", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
myTextField = [alertView textFieldAtIndex:0];
myTextField.text=mainString;
[alertView show];
[alertView release];
}
else
{
}
}
else
{
if (indexPath.row == 0)
{
}
}
}
-(void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 1)
{
NSLog(@"Cancel");
return;
}
else
{
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (cell == nil)
{
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cellCompanyId = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellCompanyId.backgroundColor = [UIColor clearColor];
cellCompanyId.font = [UIFont systemFontOfSize:18];
cellCompanyId.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellCompanyId];
cellUsername = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellUsername.backgroundColor = [UIColor clearColor];
cellUsername.font = [UIFont systemFontOfSize:18];
cellUsername.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellUsername];
cellAppversion = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellAppversion.backgroundColor = [UIColor clearColor];
cellAppversion.font = [UIFont systemFontOfSize:18];
cellAppversion.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellAppversion];
cellServer = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellServer.backgroundColor = [UIColor clearColor];
cellServer.font = [UIFont systemFontOfSize:14];
cellServer.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellServer];
}
// Set up the cell...
if(indexPath.section == 0)
{
cell.textLabel.text = [LoginSetting objectAtIndex:indexPath.row];
if (indexPath.row == 0)
{
cellCompanyId.textAlignment=NSTextAlignmentRight;
cellCompanyId.text = strCompanyid;
}
else if (indexPath.row == 1)
{
cellUsername.textAlignment=NSTextAlignmentRight;
cellUsername.text = strUsertxt;
}
else
return 0;
}
else if(indexPath.section==1)
{
cell.textLabel.text = [mic objectAtIndex:indexPath.row];
if (indexPath.row == 0)
{
UISwitch *aSwitch = [[[UISwitch alloc]init]autorelease];
aSwitch.frame=CGRectMake(215, 7, 0, 0);
[aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:aSwitch];
}
else if (indexPath.row == 1)
{
cellAppversion.textAlignment=NSTextAlignmentRight;
cellAppversion.text = @"1.3.1";
}
else if (indexPath.row == 2)
{
cellServer.textAlignment=NSTextAlignmentRight;
cellServer.text = mainString;
}
else
return 0;
}
else
{
if (indexPath.row == 0)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(LogoutEvent)
forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImage = [UIImage imageNamed:@"login_button.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
[cell addSubview:button];
}
}
//UITableViewCellAccessoryCheckmark
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 35.0;
}
我不明白为什么我的 tableViews 显示在 xcode4 中,而 xcode5 没有。我正在使用动态原型内容分组样式 UITableView。