Ios 如何使用UISwitch了解UITableview行号和节名

Ios 如何使用UISwitch了解UITableview行号和节名,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个UITableviewcell,每个单元格都有UISwitch。当我更改单元格中开关的值时,如何显示警报视图或弹出窗口,以及显示行号和节号。我想在单元格中应用函数我们显示简单警报视图 这是我的密码 @interface ADIViewController () @end @implementation ADIViewController { // NSArray* views; NSArray* countswitch; UITableView* tablevi

我有一个UITableviewcell,每个单元格都有UISwitch。当我更改单元格中开关的值时,如何显示警报视图或弹出窗口,以及显示行号和节号。我想在单元格中应用函数我们显示简单警报视图

这是我的密码

@interface ADIViewController ()

@end

@implementation ADIViewController
{
   // NSArray* views;
    NSArray* countswitch;
    UITableView* tableview;
}

- (id)initWithStyle :(UITableViewStyle)style
{

    self = [super initWithStyle :style];
    if (self)
{
        // Custom initialization
}
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    tableview.backgroundColor = [UIColor whiteColor];

    // add to canvas
    [self.view addSubview :tableview];
}

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

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection :(NSInteger)section
{
    if(section == SECTION_ID_PROFILE)
{
    return 7;
}

    if(section == SECTION_ID_SETTINGS)
{
    return 5;
}

    return 3;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection :(NSInteger)section
{
    if(section == SECTION_ID_PROFILE)
        return @"Account Profile";

    if(section == SECTION_ID_SETTINGS)
        return @"Account Settings";

    return @"Account VaxVoip";

}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath :(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"ADiCell";

    // Similar to UITableViewCell, but
    ADICell* ADiCell = (ADICell *)[theTableView dequeueReusableCellWithIdentifier :cellIdentifier];

    if (ADiCell == 0)

{
    ADiCell = [[ADICell alloc] initWithStyle :UITableViewCellStyleDefault reuseIdentifier :cellIdentifier];
}

    // Just want to test, so I hardcode the data

    NSArray* views = [[NSBundle mainBundle] loadNibNamed :@"ADiCell" owner :self options :NULL];


    ADiCell = [views objectAtIndex:0];
    NSLog(@"hy");

    for (UIView* View in views)
{
   ADiCell = (ADICell*) View;
   ADiCell.countlabel.text = [NSString stringWithFormat:@"Row: %d", [indexPath row]];


}

    return ADiCell;
}
这是一个手机代码

@implementation ADICell

@synthesize countlabel = _countlabel;
@synthesize controlleswitch = _controlleswitch;


- (IBAction)ADIcontrollerswitch :(id)sender
{
    if(self.controlleswitch.isOn == FALSE)
{
    UIAlertView* ret = [[UIAlertView alloc]
    initWithTitle :@"Vaxsoft" message :@"Hello" delegate :nil cancelButtonTitle :@"Done" otherButtonTitles :nil];

    [ret show];
}
}

- (void)setSelected :(BOOL)selected animated :(BOOL)animated
{
    [super setSelected:selected animated :animated];

    // Configure the view for the selected state
}

@end

您可以在
cellforrowatinexpath
中的每个单元格中添加开关。您可以为开关的valueChangeEvent设置选择器。考虑到你的选择器名称是“代码>开关值Value::/Cord>,你可以得到开关的超级视图,该开关是你的单元格,在该单元得到Cyter获得<代码>索引路径< /代码>之后,

- (void)switchValueChanged:(id)sender{
  UISwitch switch = (UISwitch*)sender;
  CGPoint center= switch.center; 
  CGPoint cellPoint= [sender.superview convertPoint:center toView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cellPoint];

  // Now you can use indexPath.row and IndexPath.section wh
}

在UITableDelegateMethod
-(UITableViewCell*)表视图:(UITableView*)表视图单元格FORROWATINDEXPATH:(NSIndexPath*)indexPath中添加行时,可以向
ADICell
类再添加一个属性
,您可以将您的
rowNumber=indexpath.row

    - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath :(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"ADiCell";

    // Similar to UITableViewCell, but
    ADICell* ADiCell = (ADICell *)[theTableView dequeueReusableCellWithIdentifier :cellIdentifier];

    if (ADiCell == 0)

{
    ADiCell = [[ADICell alloc] initWithStyle :UITableViewCellStyleDefault reuseIdentifier :cellIdentifier];
    ADiCell.rowNumber = indexPath.row;  // assign row number

}

    // Just want to test, so I hardcode the data

    NSArray* views = [[NSBundle mainBundle] loadNibNamed :@"ADiCell" owner :self options :NULL];


    ADiCell = [views objectAtIndex:0];
    NSLog(@"hy");

    for (UIView* View in views)
{
   ADiCell = (ADICell*) View;
   ADiCell.countlabel.text = [NSString stringWithFormat:@"Row: %d", [indexPath row]];

}

    return ADiCell;
}

现在,yopu在property
rownumber
中拥有每个单元格的行号。现在你可以随心所欲地使用它了

您可以将UISwitch子类化并向其添加自定义数据,或者更好地使用objc_setAssociatedObject()将交换机连接到indexPath,如本例所述:

借助上述代码,u可以计算单元格的实际索引路径。


因此,您可以在切换操作中使用此代码来查找实际单元格并更新单元格。

您可以为每个单元格设置一个标记,或者设置一个IndexPath。我使用了一个自定义单元格,但我不知道如何标记每个单元格cell@iOSHero你不需要给它贴标签,你可以按照我的答案找到实际的单元格。[切换superView];在ios7中不起作用。在ios7中,单元格的所有子元素都将出现在一个新的滚动视图中。您必须在实际接收开关切换操作的位置使用此代码。
CGPoint pointInTable = [switch convertPoint:switch.bounds.origin toView:_tableView];    

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];