2013-03-06 64 views
1

我是iPhone開發的新手。我有一個UITableView有三個部分,每個部分有三個行。我有一個UISegmentedControl有三個索引。tableview最初是隱藏的。當我選擇segmentIndex的任何索引時,它將顯示帶有三個部分的tableview。 但我的問題是,當我選擇分段控制則其顯示的tableView的指標只有一個部分和另外兩個部分是隱藏在tableView.How做到這一點請回答
這裏是我的代碼UITableView和UISegmentedControl

#import "DetailViewController.h" 


@interface DetailViewController() 

@end 

@implementation DetailViewController 

@synthesize tblView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    //Customize tableview 
    tblView = [[UITableView alloc] init]; 
    firstName = [NSArray arrayWithObjects:@"Parth",@"Bhadresh",@"Marshal", nil]; 
    middleName = [NSArray  arrayWithObjects:@"Dipakbhai",@"Dineshbhai",@"Mansukhbhai",nil]; 
    lastName = [NSArray arrayWithObjects:@"Patel",@"Laiya",@"Kaneria", nil]; 
    tblView.delegate = self; 
    tblView.dataSource = self; 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    sgmtControl.frame = CGRectMake(17, 180, 285, 44);  
    tblView.frame = CGRectMake(0, 0, 320, 364); 
    tblView.hidden = YES; 
    [self.view addSubview:tblView]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch (section) 
    { 
     case 0: 
      return 3; 
      break; 
     case 1: 
      return 3; 
      break; 
     case 2: 
      return 3; 
      break; 
    } 
    return 0; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section 
{ 
    switch (section) 
    { 
     case 0: 
      return @"FirstName"; 
      break; 
     case 1: 
      return @"MiddleName"; 
      break; 
     case 2: 
      return @"LastName"; 
      break; 
    } 
    return nil; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *myIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier]; 
    } 

    switch (indexPath.section) 
    { 
     case 0: 
      cell.textLabel.text =[firstName objectAtIndex:indexPath.row]; 
      break; 
     case 1: 
      cell.textLabel.text = [middleName objectAtIndex:indexPath.row]; 
      break; 
     case 2: 
      cell.textLabel.text = [lastName objectAtIndex:indexPath.row]; 
      break; 
    } 
    return cell; 
} 
-(IBAction)changeSegement:(id)sender 
{ 
    if(sgmtControl.selectedSegmentIndex == 0) 
    { 
     tblView.hidden = NO; 
    } 
    else if (sgmtControl.selectedSegmentIndex == 1) 
    { 
     tblView.hidden = NO; 
    } 
    else if (sgmtControl.selectedSegmentIndex == 2) 
    { 
     tblView.hidden = NO; 
    } 
} 
+0

重裝在每段點擊 – Rushabh 2013-03-06 06:17:12

+0

reloed表的表中的每一個部分都顯示三段在我的tableview。但是我只想顯示一個部分,另外兩個部分是隱藏的。 – 2013-03-06 06:23:58

回答

3

不喜歡這樣,

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    switch (sgmtControl.selectedSegmentIndex) 
    { 
     case 0: 
      return [firstName count]; 
      break; 
     case 1: 
      return [middleName count]; 
      break; 
     case 2: 
      return [lastName count]; 
      break; 
    } 
    return 0; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section 
{ 
    switch (sgmtControl.selectedSegmentIndex) 
    { 
     case 0: 
      return @"FirstName"; 
      break; 
     case 1: 
      return @"MiddleName"; 
      break; 
     case 2: 
      return @"LastName"; 
      break; 
    } 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *myIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier]; 
    } 

    switch (sgmtControl.selectedSegmentIndex) 
    { 
     case 0: 
      cell.textLabel.text =[firstName objectAtIndex:indexPath.row]; 
      break; 
     case 1: 
      cell.textLabel.text = [middleName objectAtIndex:indexPath.row]; 
      break; 
     case 2: 
      cell.textLabel.text = [lastName objectAtIndex:indexPath.row]; 
      break; 
    } 
    return cell; 
} 
-(IBAction)changeSegement:(id)sender 
{ 
    if(sgmtControl.selectedSegmentIndex == 0) 
    { 
     tblView.hidden = NO; 
    } 
    else if (sgmtControl.selectedSegmentIndex == 1) 
    { 
     tblView.hidden = NO; 
    } 
    else if (sgmtControl.selectedSegmentIndex == 2) 
    { 
     tblView.hidden = NO; 
    } 
    [tblView reloadData]; 
} 
+0

非常感謝。它按我的需要完美地工作。 – 2013-03-06 12:22:38

+0

@parthpatel如果你確定這個,接受答案.... – Venkat 2013-03-06 13:33:43

0

我相信我明白你想要做什麼,你想顯示不同的表格視圖單元取決於哪個按鈕段被選中。

你需要讓你的cellForRowAtIndexPath檢查選中哪個段並返回正確的一組單元。

當用戶更改分段按鈕時,請使用changeSegment:將選定的段存儲在實例變量中。然後讓它調用[self reloadData];

#import "DetailViewController.h" 


@interface DetailViewController() 
{ 
    NSArray *names; 
    NSInteger segIndex; 
} 
@property (nonatomic, strong) NSArray *names; 
@property (nonatomic) segIndex; 
@end 

@implementation DetailViewController 

@synthesize tblView; 
@synthesize names; 
@synthesize segIndex; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    //Customize tableview 
    tblView = [[UITableView alloc] init]; 
    self.names = [NSArray arrayWithObjects: [NSArray arrayWithObjects:@"Parth",@"Bhadresh",@"Marshal", nil], 
              [NSArray arrayWithObjects:@"Dipakbhai",@"Dineshbhai",@"Mansukhbhai",nil], 
              [NSArray arrayWithObjects:@"Patel",@"Laiya",@"Kaneria", nil], 
        nil]; 
    tblView.delegate = self; 
    tblView.dataSource = self; 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    sgmtControl.frame = CGRectMake(17, 180, 285, 44); 
    tblView.frame = CGRectMake(0, 0, 320, 364); 
    tblView.hidden = YES; 
    [self.view addSubview:tblView]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [self.names count]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[self.names objectAtIndex:section] count]; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section 
{ 
    switch (section) 
    { 
     case 0: 
      return @"FirstName"; 
     case 1: 
      return @"MiddleName"; 
     case 2: 
      return @"LastName"; 
    } 
    return nil; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *myIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier]; 
    } 

    cell.textLabel.text =[[self.names objectAtIndex:self.segIndex] objectAtIndex:indexPath.row]; 

    return cell; 
} 
-(IBAction)changeSegement:(id)sender 
{ 
    self.segIndex = sgmtControl.selectedSegmentIndex; 
    [self reloadData]; 
} 
+0

它的小工第一次按鈕段選中它的顯示一段和其他隱藏但是第二次顯示兩段和第三次用三段選擇按鈕段顯示錶格我想每次按鈕段選擇其顯示只有一個部分和其他兩個隱藏 – 2013-03-06 06:59:34

+0

感謝它也正在工作爐排。 – 2013-03-06 12:51:40

+0

@parthpatel不客氣。如果這是您在程序中使用的代碼,請將此答覆標記爲您的問題的答案。 – 2013-03-06 19:43:41

0

試試這個

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
switch (sgmtControl.selectedSegmentIndex) 
{ 
    case 0: 
     return 3; 
     break; 
    case 1: 
     return 3; 
     break; 
    case 2: 
     return 3; 
     break; 
} 
return 0; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section 
{ 
switch (sgmtControl.selectedSegmentIndex) 
{ 
    case 0: 
     return @"FirstName"; 
     break; 
    case 1: 
     return @"MiddleName"; 
     break; 
    case 2: 
     return @"LastName"; 
     break; 
    } 
} 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *myIdentifier = @"MyIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier]; 
if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier]; 
} 

switch (sgmtControl.selectedSegmentIndex) 
{ 
    case 0: 
     cell.textLabel.text =[firstName objectAtIndex:indexPath.row]; 
     break; 
    case 1: 
     cell.textLabel.text = [middleName objectAtIndex:indexPath.row]; 
     break; 
    case 2: 
     cell.textLabel.text = [lastName objectAtIndex:indexPath.row]; 
     break; 
} 
return cell; 
    } 
+0

謝謝你的爐排 – 2013-03-06 13:01:38

1

在山獅的代碼稍加修改,使之更加通用,並刪除開關

@synthesize currentArray; 
. 
. 
. 
. 

- (void)viewDidLoad 
{ 
     . 
     . 
     . 
     //ALLOCATE currentArray 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return currentArray.count; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section 
{ 
    switch (sgmtControl.selectedSegmentIndex) 
    { 
     case 0: 
      return @"FirstName"; 
      break; 
     case 1: 
      return @"MiddleName"; 
      break; 
     case 2: 
      return @"LastName"; 
      break; 
    } 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *myIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier]; 
    } 

    cell.textLabel.text =[currentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 
-(IBAction)changeSegement:(id)sender 
{ 
    if(sgmtControl.selectedSegmentIndex == 0) 
    { 
     currentArray = firstName; 
    } 
    else if (sgmtControl.selectedSegmentIndex == 1) 
    { 
     currentArray = middleName; 
    } 
    else if (sgmtControl.selectedSegmentIndex == 2) 
    { 
     currentArray = lastName; 
    } 
    tblView.hidden = NO; 
    [tblView reloadData]; 
} 
0
//You may get hint from my code 
    //UISegmentedControl with TableView using Objective C 

    //complete working code 

@interface TabTwoScheduleViewController() <UITableViewDelegate ,  UITableViewDataSource> 
{ 
CGRect rect; 
NSArray *list; 
NSArray *list1; 
NSArray *list2; 
NSArray *list3; 
NSArray *list4; 
UITableView *segmentTableView; 
} 
@end 

@implementation TabTwoScheduleViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 

rect = [[UIScreen mainScreen]bounds]; 

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 10, rect.size.width-20, rect.size.height/10-23)]; 
scroll.contentSize = CGSizeMake(rect.size.width, rect.size.height * 2); 
scroll.showsHorizontalScrollIndicator = YES; 
scroll.backgroundColor = [UIColor yellowColor]; 

NSArray *itemArray = [NSArray arrayWithObjects: @"ONLINE", @"CLASSROOM", @"WEBCASTING", nil]; 
list = [NSArray  arrayWithObjects:@"list.1",@"list.2",@"list.3",@"list.4",@"list.5", nil]; 
list1 = [NSArray arrayWithObjects:@"list1.1",@"list1.2",@"list1.3",@"list1.4",@"list1.5", nil]; 
list2 = [NSArray arrayWithObjects:@"list2.1",@"list2.2",@"list2.3",@"list2.4",@"list2.5", nil]; 
list3 = [NSArray arrayWithObjects:@"list3.1",@"list3.2",@"list3.3",@"list3.4",@"list3.5", nil]; 
list4 = [NSArray arrayWithObjects:@"list4.1",@"list4.2",@"list4.3",@"list4.4",@"list4.5", nil]; 

// segmentedControl is declared as property 
self.segmentedControl = [[UISegmentedControl alloc]  initWithItems:itemArray]; 
self.segmentedControl.frame = CGRectMake(0, 0, rect.size.width-20, 50); 
self.segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain; 
[self.segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents:  UIControlEventValueChanged]; 
self.segmentedControl.selectedSegmentIndex = 0; 
[scroll addSubview:self.segmentedControl]; 
[self.view addSubview:scroll]; 

//ADDING TABLEVIEW OVER VIEW(I added this view to get leading and trailing space for tableViewCell) 

UIView *vw = [[UIView alloc]initWithFrame:CGRectMake(0, 70, rect.size.width, rect.size.height)]; 
    vw.backgroundColor = [UIColor redColor]; 
[self.view addSubview:vw]; 

    //TABLE VIEW 
segmentTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, rect.size.width-20, rect.size.height-230) style:UITableViewStylePlain]; 
segmentTableView.backgroundColor = [UIColor yellowColor]; 

segmentTableView.delegate = self; 
segmentTableView.dataSource = self; 

[vw addSubview:segmentTableView]; 
    } 

// number of sections for tableView 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView 
{ 
NSInteger a=0; 
switch (self.segmentedControl.selectedSegmentIndex) 
{ 
    case 0: 
     a=list.count; 
    break; 

case 1: 
    a=list1.count; 
    break; 

case 2: 
    a=list1.count; 
    break; 

    default: 
    a=list1.count; 
    break; 
} 
    return a; 
} 
// number of row in the section 
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section 
{ 
return 1; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *simpleTableIdentifier = @"ScheduleCustomTableViewCell"; 

//ScheduleCustomTableViewCell is name of custom TabelViewCell 
ScheduleCustomTableViewCell *cell =(ScheduleCustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) 
{ 
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleCustomTableViewCell" owner:self options:nil]; 
cell = [nib objectAtIndex:0]; 
} 

// conditions to get different values on label 
if (self.segmentedControl.selectedSegmentIndex==0) 
{ 
cell.labelAddress1.text = [list objectAtIndex:indexPath.section]; 
cell.labelAddress2.text = [list1 objectAtIndex:indexPath.section]; 
} 
else if (self.segmentedControl.selectedSegmentIndex==1) 
{ 
cell.labelAddress1.text = [list1 objectAtIndex:indexPath.section]; 
cell.labelAddress2.text = [list2 objectAtIndex:indexPath.section]; 
} 
else 
{ 
cell.labelAddress1.text = [list2 objectAtIndex:indexPath.section]; 
cell.labelAddress2.text = [list3 objectAtIndex:indexPath.section]; 
} 
cell.backgroundColor = [UIColor yellowColor]; 

return cell ; 
} 
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
return 130; 
} 
// Header is given to get spacing between TableViewCells 
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
return 10; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
UIView *headerView = [[UIView alloc] init]; 
headerView.backgroundColor = [UIColor redColor]; 
return headerView; 
} 

    - (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 

}

- (void)MySegmentControlAction:(UISegmentedControl *)segment 
{ 
[segmentTableView reloadData]; 
} 

@end