我有一个带有每个单元的UISwitch的UITableviewcell。当我更改单元格中的开关值时,如何显示警报视图或弹出窗口并显示行号和要在单元格中显示的部分我想在单元格中显示简单的警报视图。
这是我的代码
@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
答案 0 :(得分:0)
您可以在CellForRowAtIndexPath
中的每个单元格中添加开关。你可以为switch的valueChangeEvent设置选择器。考虑您的选择器名称为switchValueChanged:
,并且在单元格为该单元格获取indexPath
后,您可以获得将成为您的单元格的开关的超级视图。
- (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
}
答案 1 :(得分:0)
您可以向ADICell
课程添加一个属性当您在UITableDelegateMethod - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath :(NSIndexPath *)indexPath
中添加行时,您可以放置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拥有属性rownumber
中每个单元格的行号。现在您可以根据需要使用它。
答案 2 :(得分:0)
您可以将UISwitch子类化并向其添加自定义数据,或者更好地使用objc_setAssociatedObject()将交换机连接到indexPath,如本示例中所述:http://kingscocoa.com/tutorials/associated-objects/
答案 3 :(得分:0)
CGPoint pointInTable = [switch convertPoint:switch.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
在上述代码的帮助下,你可以计算出单元格的实际索引路径。
因此,您可以在切换操作中使用此代码来查找更新单元格的实际单元格。