如何解决如何在UITableView中使用UISegmentedControl
| 我想分为两个部分,像这样 降级段将在tableView中显示降级苍蝇,复出段将显示复出苍蝇。能否请我解释一下我该怎么做?我应该制作2个tableView还是仅制作一个?谢谢解决方法
您可以为此使用一个UITableView并在segmentcontrolindexchange方法上重新加载表数据。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{ if(segment.selectedSegmentIndex==0)
{
return [List count];
}
else
if (segment.selectedSegmentIndex==1) {
return[List1 count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @\"Cell\";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200,10,100,20) ];
// Configure the cell...
lbl =[[UILabel alloc]initWithFrame:CGRectMake(100,20) ];
if(segment.selectedSegmentIndex==0)
{
cell.textLabel.text=[List objectAtIndex:indexPath.row];
lbl.text = [List3 objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl];
lbl1.text = [List objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl1];
}
else if(segment.selectedSegmentIndex==1) {
cell.textLabel.text=[List1 objectAtIndex:indexPath.row];
lbl.text = [List objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl];
}
return cell;
}
-(IBAction) segmentedControlIndexChanged
{
switch (self.segment.selectedSegmentIndex) {
case 0:
i=0;
[table reloadData];
break;
case 1:
i=1;
[table reloadData];
default:
break;
}
}
,您可以通过任何一种方式进行操作...一个UITableView将需要您更改数据源以及细分控件的更改时间。另外,您也可以选择使用两个UITableView \以及它们自己的控制器,并使用细分控件轻松切换它们的可见性。
,遵循Apple设计准则的另一种方法是用工具栏控件替换分段控件,然后可以使用UIToolBarController中的构建来管理UI堆栈并为每个状态提供不同的视图。
, My case was also same (UISegment Control and UITableView with two prototype cells.
class DashBoardViewController: UIViewController,UITableViewDataSource {
@IBOutlet weak var dashBoardSegment: UISegmentedControl!
@IBOutlet weak var dashBoardTableView: UITableView!
//TableView Variables
var CellIdentifier: String = \"dashBoardTableReportsCellID\"
var cell:UITableViewCell?
var rowCount:Int?
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
switch CellIdentifier
{
case \"dashBoardTableCellID\":
rowCount = 2
case \"dashBoardTableReportsCellID\":
rowCount = 4
default:
break;
}
return rowCount!
}
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch CellIdentifier
{
case \"dashBoardTableCellID\":
cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)!
print(\"CELL 1\")
case \"dashBoardTableReportsCellID\":
cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)!
print(\"CELL 2\")
default:
break;
}
return cell!
}
// OR
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (CellIdentifier == \"dashBoardTableCellID\")
{
let surveyCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! DashBoardTableViewCell
surveyCell.itemDetailLabel.text = \"Survey Cell Title\"
return surveyCell
}
else
{
let reportsCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier)! as! DashBoardTableViewReportsCell
return reportsCell
}
}
//Mark:- Segment Control Action
@IBAction func dashBoardSegmentValueChanged(sender: AnyObject) {
switch dashBoardSegment.selectedSegmentIndex
{
case 0:
CellIdentifier = \"dashBoardTableCellID\"
self.dashBoardTableView.reloadData()
case 1:
CellIdentifier = \"dashBoardTableReportsCellID\"
self.dashBoardTableView.reloadData()
default:
break;
}
}
}
,//UISegmentedControl with TableViewController
//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,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,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,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。