如何在UITableView中使用UISegmentedControl

如何解决如何在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 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res