我有三种不同类型的tableview, 如何使用分段控制切换到差异tableview?
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
tableData1 = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f",@"a", @"b", @"c", @"d", @"e", @"f", nil];
tableData2 = [[NSMutableArray alloc] initWithObjects: @"c", @"d", @"e", @"f",@"a", @"b", @"c", @"d", @"e", @"f",@"a", @"b", nil];
thumbnails1 = [[NSMutableArray alloc] initWithObjects:@"a.png", @"b.png", @"c.png", @"d.png", @"e.png", @"f.png",@"a.png", @"b.png", @"c.png", @"d.png", @"e.png", @"f.png", nil];
thumbnails2 = [[NSMutableArray alloc] initWithObjects:@"d.png", @"e.png", @"f.png",@"a.png", @"b.png", @"c.png",@"a.png", @"b.png", @"c.png", @"d.png", @"e.png", @"f.png", nil];
}
并在tableview委托中:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.seg.selectedSegmentIndex==0) {
return [tableData1 count];
}
else if(self.seg.selectedSegmentIndex==1)
{
return [tableData2 count];
}
}
return [tableData1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"TableViewCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.seg.selectedSegmentIndex==0)
{
cell.nameLabel.text=[tableData1 objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [thumbnails1 objectAtIndex:indexPath.row];
}
else if(self.seg.selectedSegmentIndex==1)
{
cell.nameLabel.text=[tableData2 objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [thumbnails2 objectAtIndex:indexPath.row];
}
return cell;
}
在DidSelectRow部分:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detail = [[DetailViewController alloc] init];
if (self.seg.selectedSegmentIndex==0)
{
[detail setTitle:[tableData1 objectAtIndex:indexPath.row]];
detail.DetailLabel.text = [tableData1 objectAtIndex:indexPath.row];
detail.DetailImage.image = [thumbnails1 objectAtIndex:indexPath.row];
}
else if(self.seg.selectedSegmentIndex==1)
{
[detail setTitle:[tableData2 objectAtIndex:indexPath.row]];
detail.DetailLabel.text = [tableData2 objectAtIndex:indexPath.row];
detail.DetailImage.image = [thumbnails2 objectAtIndex:indexPath.row];
}
[self.navigationController pushViewController:detail animated:YES];
}
答案 0 :(得分:0)
我编辑了代码......你犯了很多小错误......这是你急需的代码......
homePage.h
#import <UIKit/UIKit.h>
@interface homePage : UIViewController
@property (strong, nonatomic) IBOutlet UISegmentedControl *seg;
@property (strong, nonatomic) IBOutlet UITableView *table;
@property (nonatomic,retain)NSMutableArray*tabledata1;
@property (nonatomic,retain)NSMutableArray*tabledata2;
@property (nonatomic,retain)NSMutableArray*thumbnails1;
@property (nonatomic,retain)NSMutableArray*thumbnails2;
- (IBAction)segment:(id)sender;
@end
homepage.m
@implementation homePage
@synthesize tabledata1,tabledata2,thumbnails1,thumbnails2;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tabledata1=[[NSMutableArray alloc]initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f",@"a", @"b", @"c", @"d", @"e", @"f", nil];
tabledata2=[[NSMutableArray alloc]initWithObjects:@"c", @"d", @"e", @"f",@"a", @"b", @"c", @"d", @"e", @"f",@"a", @"b", nil];
thumbnails1=[[NSMutableArray alloc]initWithObjects:@"a.jpg", @"b.jpg", @"c.jpg", @"d.jpg", @"e.jpg", @"f.jpg",@"a.jpg", @"b.jpg", @"c.jpg", @"d.jpg", @"e.jpg", @"f.jpg", nil];
thumbnails2=[[NSMutableArray alloc]initWithObjects:@"d.jpg", @"e.jpg", @"f.jpg",@"a.jpg", @"b.jpg", @"c.jpg",@"a.jpg", @"b.jpg", @"c.jpg", @"d.jpg", @"e.jpg", @"f.jpg", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.seg.selectedSegmentIndex==0)
{
return [tabledata1 count];
}
else
{
return [tabledata2 count];
}
return [tabledata1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.seg.selectedSegmentIndex==0) {
cell.thumbnailImageView.image=[UIImage imageNamed:[thumbnails1 objectAtIndex:indexPath.row]];
cell.nameLabel.text=[tabledata1 objectAtIndex:indexPath.row];
}
else if(self.seg.selectedSegmentIndex==1)
{
cell.thumbnailImageView.image=[UIImage imageNamed:[thumbnails2 objectAtIndex:indexPath.row]];
cell.nameLabel.text=[tabledata2 objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailPage*detail=[[detailPage alloc]init];
if (self.seg.selectedSegmentIndex==0)
{
detail.DetailLabel_string=[tabledata1 objectAtIndex:indexPath.row];
detail.DetailImage_string=[thumbnails1 objectAtIndex:indexPath.row];
}
else if(self.seg.selectedSegmentIndex==1)
{
detail.DetailLabel_string=[tabledata2 objectAtIndex:indexPath.row];
detail.DetailImage_string=[thumbnails2 objectAtIndex:indexPath.row];
}
[self.navigationController pushViewController:detail animated:YES];
}
- (IBAction)segment:(id)sender
{
[self.table reloadData];
}
detailPage.h
#import <UIKit/UIKit.h>
@interface detailPage : UIViewController
@property(nonatomic,retain)NSString*DetailLabel_string;
@property(nonatomic,retain)NSString*DetailImage_string;
// and also synthesis this two string its necessary.
@property (strong, nonatomic) IBOutlet UILabel *tittlelbl;
@property (strong, nonatomic) IBOutlet UIImageView *imageview;
@end
detailpage.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"%@",DetailImage_string);
NSLog(@"%@",DetailLabel_string);
self.tittlelbl.text=DetailLabel_string;
self.imageview.image=[UIImage imageNamed:DetailImage_string];
}
它完全适合我....我做了同样的事情,你可能想要......也关心定制细胞......