如何在动画下来的UITableView上添加元素

时间:2011-07-05 04:51:05

标签: iphone uitableview viewcontroller

基本上,我试图以编程方式在UITableView上面添加一个View。下面的代码,将UITableView设置为150像素的动画,我想在上面创建的空间中添加元素。但是无论何时我尝试添加某些内容,比如代码中的Label,它就会消失。它是一个UITableViewController,所以我想这可能与它有关。

帮助!

- (IBAction)chooseCity:(id)sender {

[UIView beginAnimations:nil context: NULL];
[UIView setAnimationDuration:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
CGRect frame = self.tableView.frame;
if (frame.origin.y > 0) {
    frame.origin.y = 0;
} else {
    frame.origin.y += 150;        
}
[self.tableView setFrame:frame];
[UIView commitAnimations];

UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, 200, 30)];
testLabel.text = @"Hello";
testLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:testLabel];

}

3 个答案:

答案 0 :(得分:1)

如果要在视图顶部添加UILabel,可以将其添加到tableview的tableHeaderView属性中,

分配UILabel并将其分配给tableHeaderView,即使您可以将此部分放入UIView动画中,也可以根据您的要求进行动画制作。

答案 1 :(得分:1)

你可以使用

UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, 200, 30)];
testLabel.text = @"Hello";
testLabel.backgroundColor = [UIColor redColor];

tableView.tableHeaderView = testLabel;
[testLabel release];
//to add in footer tableView.tableFooterView = testLabel;

根据您的情况,您可以将其设置为nil

tableView.tableHeaderView = testLabel;

答案 2 :(得分:0)

将在

中实施
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return someViewIWantInMyHeader;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {

         return someSize;
    }
}

不适合你吗?