[英]How to insert a UITableViewCell at the beginning of UITableView
我試圖建立一個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];
}
您可以在表視圖中添加“標題”。
在您的tableview類中:
self.tableView.tableHeaderView = yourView;
您不能只在表中的UItable上方添加一行。 如果只需要一行文本,為什么不根據需要使用UITextField,UILabel或UITextView,並將其放在UItable上方即可。
如果我誤解了您,而您只是想在第一部分中添加一行作為第一行,那么您需要做的所有事情如下:
if (section == 0) {
return [[letterDictionary objectForKey:currentLetter] count]+1;
} else
{
return [[letterDictionary objectForKey:currentLetter] count];
}
並確保在返回索引路徑的行時,您也具有類似的if語句,並返回section == 0和row == 0所需的內容。
但是,如果您向下滾動表格視圖,則第一行肯定會滾遠-如前所述,我不確定您到底需要什么。
您可以嘗試自定義tableview部分的標題...
例如,您可以使用以下代碼:
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下進行自定義,則可以使用相同的方法來更改Header和Footer。
最后看看有關這些方法的文檔:
-tableView:viewForHeaderInSection:
-tableView:heightForHeaderInSection:
-tableView:viewForFooterInSection:
-tableView:heightForFooterInSection:
希望這符合您的需求!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.