Iphone 如何在UITableView的开头插入UITableViewCell

Iphone 如何在UITableView的开头插入UITableViewCell,iphone,ios,uitableview,Iphone,Ios,Uitableview,我正在尝试设置一个UITableView,其中包含x个部分和x个每个部分的行数 但是,我想在我的UITableView顶部添加一行,是否有办法将其硬编码到视图中 我目前根据NSdictionary返回每个节的节数和行数,如下所示 - (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView { // Return the number of sections. return [letterDictionary

我正在尝试设置一个UITableView,其中包含x个部分和x个每个部分的行数

但是,我想在我的
UITableView
顶部添加一行,是否有办法将其硬编码到视图中

我目前根据NSdictionary返回每个节的节数和行数,如下所示

- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView
{
    // Return the number of sections.
    return [letterDictionary count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // returns the number of rows per section based off each entry in letterDictionary   
    currentLetter = [sectionLetterArray objectAtIndex:section];
    return [[letterDictionary objectForKey:currentLetter] count];       
}

不能只在表中的UItable上方添加一行。如果您只需要一行文本,为什么不根据需要使用UITextField、UILabel或UITextView,并将其放置在UItable上方您喜欢的任何位置

如果我误解了你的意思,而你只是想在第一节中添加一行作为第一行,那么你只需要这样做:

if (section == 0) {
  return [[letterDictionary objectForKey:currentLetter] count]+1;
} else
{
  return [[letterDictionary objectForKey:currentLetter] count];
}
并确保在为indexpath返回行时,也有一个类似的if语句,并返回section==0和row==0所需的任何内容


但是如果你向下滚动你的表视图,第一行肯定会滚走——正如我所说的,我不确定你到底需要什么。

你可以尝试自定义你的表视图部分的标题…
例如,您可以使用如下内容:

YourController.h
-(UIView *)headerView;

YourController.m
-(UIView *)headerView
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectZero];
    // Place everything you want in your header
    // using [header addSubview:yourSubview];
    // Finally set header's frame and return it
    header.frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
    return header;
}

// Use this to return header's height
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == yourSection)
    {
        return [self headerView].frame.size.height;
    }
    else
    {
        return [self sectionHeaderHeight];
    }
}

// Use this to return your view
-(UIView *)tableView:(UITableVIew *)tableVIew viewForHeaderInSection:(NSInteger)section
{
    if (section == yourSection)  // To show the header only on a specified section
    {
        return [self headerView];
    }
    else
    {
        return nil;
    }
}
如果您改变主意并希望在tableView下进行自定义,您可以使用相同的方法更改页眉和页脚。
最后看一下关于这些方法的文档:



希望这符合你的需要

您可以在tableview中添加“标题”

在tableview类中:

self.tableView.tableHeaderView = yourView;

因为我希望它看起来像tableview中的一个条目。。因为输入的数据是动态的,用于创建节和行,所以我认为唯一的其他方法是插入字典的开头。。但是我想我甚至做不到。在你的最后一句话里,你说的那句话会卷走,这正是我想要的。它只需要在一开始。基本上,我有一个条目的表格视图,如果用户想要选择这个按钮的所有功能,我建议您重新设计您的UI,以便在表格上方有一个带有OK按钮的输入区域。按OK时,用户刚刚提供的文本将转到需要的任何位置。您可以使用[table reloadData],一旦您将用户提供的文本插入到表的底层数据结构中,您的表视图将是最新的。因此,您只需要在第一部分的第一行中显示一个按钮式的行为,这就是选择all?虽然这在我描述的添加“==0”检查的方法中肯定是可能的。我现在甚至建议你考虑重新设计UI -为什么不只是添加一个按钮-总是可见的,它选择所有的线(不明白为什么它不应该看到一旦你向下滚动表)-这肯定是更多的iPhone样的行为。好了,谢谢帮助,我想我会这样做。这将节省很多问题。感谢您的回复,但我已经为每个部分(按字母顺序的部分)提供了一个部分标题。。。因此,我想我必须使用导航栏下方但tableview上方的按钮状单元格。。因此,tableview可以在其下滚动。。牢房就在上面。