UITableView混合部分项目

时间:2014-06-23 08:15:00

标签: ios uitableview

我正在尝试从我的tableview的前两个部分打开两个不同的Viewcontrollers,并从其他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];
    }

}
}

2 个答案:

答案 0 :(得分:1)

这是你问题的答案:

@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的解释。

答案 1 :(得分:0)

UITableView

的数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

用于定义UITableView的节数,每个节有不同的行从0,1,2 .....

开始

在您的情况下,您尝试使用不同的indexPath.section,因为它应该只使用1 section,对于多个部分,应该有多个数据源来定义部分。

由于您已经获得了解决方案,我只想澄清indexPaths rows and sections的概念。

感谢。