0
我需要添加另一節到我的tableview,這將發生在表的頂部。我也想使用自定義視圖,所以我可以很好地格式化內容。如何向UITableView添加一次只有部分和節標題?
在下圖中,您可以看到每個下方都有節標題和行。
我試了幾次,但我有點困惑,我應該有多少部分,並顯示所有我需要的行的邏輯。爲了進一步複雜化,我還顯示了一個單獨的行,文字說沒有可用的行。在這種情況下,我想顯示我的新部分和文本。
這裏是我的代碼..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int count = [sectionTitles count];
if (count == 0) {
return 1;
} else {
return count;
}
}
//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
int count = 0;
if (sectionRowCounts.count != 0) {
NSString *title = [sectionTitles objectAtIndex:section];
count = [[sectionRowCounts objectForKey:title] intValue];
}
return (count ? count : 1);
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section {
NSString *title = @"";
if (sectionTitles.count != 0) {
title = [sectionTitles objectAtIndex:section];
}
return title;
}
//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
(NSInteger)section {
if (sectionTitles.count == 0) {
UIView *customView = [[[UIView alloc] init] autorelease];
return customView;
}
HeaderSectionReportView *customView = [[[HeaderSectionReportView alloc]
initWithFrame:CGRectMake(0.0, 0.0, 360.0, 20.0)] autorelease];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.frame = CGRectMake(10.0, 0.0, 100.0, 20.0);
headerLabel.text = [sectionTitles objectAtIndex:section];
[customView addSubview:headerLabel];
[headerLabel release];
return customView;
}
- (CGFloat) tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {
return 20.0;
}
//
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell1;
if(rowSections.count == 0) {
cell1 = [[[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
UIView *view = [[[UIView alloc] init] autorelease];
view.backgroundColor = [UIColor clearColor];
cell1.selectedBackgroundView = view;
cell1.textLabel.text = @"No transactons found";
return cell1;
}
NSString *strRowSection = [NSString stringWithFormat:@"%i-%i",
indexPath.section, indexPath.row];
NSInteger intDBRow = [rowSections indexOfObject:strRowSection];
static NSString *CellIdentifier = @"ReportCell";
ReportCell *cell = (ReportCell *) [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
//
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"ReportCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = ((ReportCell *) currentObject);
break;
}
}
}
NSString *desc = [[transactionsArray objectAtIndex:intDBRow]
objectForKey:@"desc"];
cell.descriptionValue.text = desc;
UIView *view = [[[UIView alloc] init] autorelease];
view.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = view;
return cell;
}
//
- (void) populateDataSource {
int sectionIncr = -1;
int rowIncr = 0;
sectionTitles = [[NSMutableArray alloc] initWithObjects:nil];
sectionRowCounts = [[NSMutableDictionary alloc] init];
rowSections = [[NSMutableArray alloc] initWithObjects:nil];
NSMutableArray *arrayTmp= [[NSMutableArray alloc] init];
//populate array
self.transactionsArray = arrayTmp;
[arrayTmp release];
}