我试图从我的表格 View 的前 2 个部分中打开 2 个不同的 View Controller ,并从其他 4 个部分中打开一些 URL。第一部分不执行任何操作,其他部分混淆,并且用于打开 URL 的部分不起作用。感谢您的回复。
@implementation SettingsViewController {
NSArray * sectionheaderArr;
}
- (void)viewDidLoad
{
sectionheaderArr = [[NSArray alloc]initWithObjects:NSLocalizedString(@"fb",
nil),NSLocalizedString(@"recommend", nil),NSLocalizedString(@"feedback",
nil),NSLocalizedString(@"rate", nil),NSLocalizedString(@"language",
nil),NSLocalizedString(@"all_providers", nil), nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
return sectionheaderArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier =@"Cell";
int row = indexPath.row;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [sectionheaderArr objectAtIndex:row];
return cell;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.section == 0) {
if (indexPath.row ==0){
[self performSegueWithIdentifier:@"openFacebook" sender:self];
}
}
if (indexPath.section ==1) {
if (indexPath.row ==1) {
[self performSegueWithIdentifier:@"opentest" sender:self];
}
}
if (indexPath.section==2) {
if (indexPath.row ==2) {
UIApplication*app1 = [UIApplication sharedApplication];
NSString*path = @"http://www.google.de";
NSURL*myurl = [NSURL URLWithString:path];
[app1 openURL:myurl];
}
}
}
最佳答案
这是您问题的答案:
@implementation SettingsViewController {
NSArray * sectionheaderArr;
}
- (void)viewDidLoad
{
sectionheaderArr = [[NSArray alloc]initWithObjects:NSLocalizedString(@"fb",
nil),NSLocalizedString(@"recommend", nil),NSLocalizedString(@"feedback",
nil),NSLocalizedString(@"rate", nil),NSLocalizedString(@"language",
nil),NSLocalizedString(@"all_providers", nil), nil];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
return sectionheaderArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier =@"Cell";
int row = indexPath.row;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [sectionheaderArr objectAtIndex:row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.row ==0){
[self performSegueWithIdentifier:@"openFacebook" sender:self];
}
if (indexPath.row ==1) {
[self performSegueWithIdentifier:@"opentest" sender:self];
}
if (indexPath.row ==2) {
UIApplication*app1 = [UIApplication sharedApplication];
NSString*path = @"http://www.google.de";
NSURL*myurl = [NSURL URLWithString:path];
[app1 openURL:myurl];
}
}
请注意,在您实现“didDeselectRowAtIndexPath”时,我还更改了“didSelectRowAtIndexPath”方法,因此在您取消选择单元格之前该方法不会起作用。
也感谢@iphonic 的解释。
关于ios - UITableView 混合部分项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361236/