我是iOS新手。我正在使用storyboard进行UI开发。我创建了一个带有标签和活动加载器的自定义标题单元格。现在我想在开关关闭时为第1节的标题单元设置动画。我尝试了足够但我无法做到这一点。 但是,当我只为第1节创建一个自定义标题时,单元格就是动画。但是我想重新使用那个单元格。当我这样做,即当我为两个部分创建自定义标题时,单元格不是动画,甚至活动加载器也停止动画。当我为两个部分创建单元格标题时,帧值返回(0,0,600,44)和(0,0,600,44)但是当为section-1创建时它只返回(0,98,383,44),为什么在这种情况下宽度较少,为什么为两个部分创建的标题不是动画。 请帮帮我。
以下是代码段:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"bluetoothCell"];
UISwitch *addswitch=nil;
if (addswitch==nil)
{
addswitch=[[UISwitch alloc]initWithFrame:(CGRectZero)];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
cell.accessoryView=addswitch;
[addswitch addTarget:self action:@selector(bluetoohSwitchClicked:) forControlEvents:UIControlEventValueChanged ];
cell.textLabel.text=@"Bluetooth";
}
}
else if (indexPath.section==1) {
cell.textLabel.text=@"bluetooth devices";
cell.accessoryView=nil;
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
HeaderTableViewCell *customheader;
NSLog(@"section.. %ld",(long)section);
customheader=[ tableView dequeueReusableCellWithIdentifier:@"headercell"];
self.customheader=customheader;
customheader.autoresizesSubviews=YES;
if (section==0) {
customheader.activityloader2.hidden=NO;
customheader.tag=0;
NSLog(@"section %ld,%@",(long)section,customheader);
} else if(section==1) {
customheader.activityloader2.hidden=NO;
customheader.tag=1;
NSLog(@"sections %ld,%@",(long)section,customheader);
}
return customheader;
}
-(void)bluetoohSwitchClicked:(id) sender
{
UISwitch* switchControl = sender;
if ([switchControl isOn])
{
self.onoffswitch=@"ON";
[switchControl setOn:YES animated:YES];
}
else
{
self.onoffswitch=@"OFF";
[switchControl setOn:NO animated:YES];
}
[self bluetoothStatechanged:[self.onoffswitch isEqualToString:@"ON"]?YES:NO];
}
-(void)bluetoothStatechanged:(BOOL)on
{
HeaderTableViewCell* customheader=(HeaderTableViewCell *)[self tableView:self.maintableview2 viewForHeaderInSection:1];
__block CGRect frame=customheader.frame;
NSLog(@"bluetoothStatechanged 2 %f,%f,%@",frame.origin.y,frame.size.height,customheader);
if (on==YES)
{
[customheader.activityloader2 startAnimating];
customheader.alpha = 0.0 ;
frame.origin.y-=frame.size.height;
NSLog(@"frame1 %@",customheader);
customheader.frame=frame;
NSLog(@"frame %@",customheader);
[UIView animateWithDuration:.2f animations:^{
customheader.alpha = 0.1 ;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0f animations:^{
frame.origin.y+=frame.size.height;
customheader.frame=frame;
customheader.alpha = 1.0 ;
}];
}];
}
else
{
[customheader.activityloader2 stopAnimating];
customheader.alpha = 1.0 ;
[UIView animateWithDuration:0.2f animations:^{
customheader.alpha = 0.9 ;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0f animations:^{
frame.origin.y-=frame.size.height;
customheader.frame=frame;
customheader.alpha = 0.0 ;
} completion:^(BOOL finished) {
frame.origin.y+=frame.size.height;
customheader.frame=frame;
}];
}];
}
}
但是,如果我改变了像bellow那样的cellForRowAtIndexPath函数,那么它会在第二部分创建标题,即section-1,并且活动指示符也在工作,但是头部不是在第一部分创建的,即第0部分。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
HeaderTableViewCell *customheader;
NSLog(@"section.. %ld",(long)section);
if(self.customheader==nil)
{
customheader=[ tableView dequeueReusableCellWithIdentifier:@"headercell"];
self.customheader=customheader;
}else
{
customheader=self.customheader;
}
...// all is same as written above
}