---
title: "UISwitch 初始化"
author: "修博龙泉"
date: "2012-09-25T18:54:30Z"
tags: []
categories: ["移动开发"]
slug: "uiswitch"
draft: false
lastmod: "2012-09-25T19:14:17Z"
id: 1685278
url: "https://longquan.iteye.com/blog/1685278"
---

```java
1.UISwitch的初始化
UISwitch *switchView = [[uiswitch alloc] initWithFrame:CGRectMake(54.0f, 16.0f, 100.0f, 28.0f)];
2.设置UISwitch的初始化状态
switchView.on = YES;//设置初始为ON的一边
 3.UISwitch事件的响应
[switchview addtarget:self action:@selector(switchaction:) forcontrolevents:uicontroleventvaluechanged];
```


在cell中添加uiswitch

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = ;
    if (cell == nil) {
        cell =  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if(==0){
        cell.textLabel.text=@"新消息通知";
    }else if(==1){
        cell.textLabel.text=@"成员可邀请";
    }else if(==2){
        cell.textLabel.text=@"是否公开";
    }
    
    UISwitch *switchview =  initWithFrame:CGRectZero];
    switchview.tag = ;
    ;
    cell.accessoryView = switchview;
    ;
    return cell;
    
}