我想實現展開/摺疊表視圖單元格,如解釋here。我的修改是我希望這是一個完整的展開/摺疊表視圖。所以每個部分都可以摺疊/展開。我看着蘋果的例子,這似乎是一個矯枉過正的問題。凌亂UITableViewCell與展開/摺疊
我目前面臨的問題主要是由於格式和顏色問題。功能似乎很好。我希望部分標題具有白色背景,行要具有藍色背景。這裏是我的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (dimension.selectedSegmentIndex == 0){
if (!indexPath.row)
{
cell.textLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
cell.textLabel.text = [timeslot objectAtIndex:indexPath.section];
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else
{
// all other rows
cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
cell.textLabel.textColor = [UIColor whiteColor];
NSArray *listItems = [[timeslot objectAtIndex:indexPath.section] componentsSeparatedByString:@":"];
if (indexPath.row == 1){
cell.textLabel.text = [NSString stringWithFormat:@"%@:00 - %@:15", [listItems objectAtIndex:0], [listItems objectAtIndex:0]];
}
if (indexPath.row == 2){
cell.textLabel.text = [NSString stringWithFormat:@"%@:15 - %@:30", [listItems objectAtIndex:0], [listItems objectAtIndex:0]];
}
if (indexPath.row == 3){
cell.textLabel.text = [NSString stringWithFormat:@"%@:30 - %@:45", [listItems objectAtIndex:0], [listItems objectAtIndex:0]];
}
if (indexPath.row == 4){
cell.textLabel.text = [NSString stringWithFormat:@"%@:45 - %d:00", [listItems objectAtIndex:0], ([[listItems objectAtIndex:0] intValue] + 1)];
}
}
}
if (dimension.selectedSegmentIndex == 1){
Discount * discount = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = discount.vendor;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[manager loadObjectsAtResourcePath:@"/route.json" objectClass:[Location class] delegate: self];
RouteMapViewController * routemap = [[RouteMapViewController alloc] initWithNibName:@"RouteMapViewController" bundle:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style: UIBarButtonItemStyleBordered
// style: UIBarButtonItemStylePlain
// style: UIBarButtonItemStyleDone
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
if (dimension.selectedSegmentIndex == 0){
if (!indexPath.row)
{
// only first row toggles expand/collapse
[preference deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:preference numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:preference numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[preference deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else
{
[preference insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}
}
這裏就是我希望它看上去像(該部分的字體是錯誤的,因爲我已經將它設置爲Arial粗體,但它不是在這裏)
這就是我得到擴大底部區域時:
這個爛攤子開始之後我滾動UITableV發生瀏覽以擴展後面的條目...我想它與更新視圖有關。
我們無法看到圖像?你使用viewForSection方法嗎? – Ravin 2011-04-14 03:14:26
不..沒有viewForSection方法。你指的是什麼圖像? – aherlambang 2011-04-14 03:23:48
由於我無法看到圖像,請告訴您發生了什麼奇怪的事情? – Ravin 2011-04-14 03:27:03