【发布时间】:2011-08-05 04:57:49
【问题描述】:
我正在尝试按照here 的说明实现展开/折叠表格视图单元格。我所做的修改是我希望这是一个完整的展开/折叠表视图。所以每个部分都可以折叠/展开。我查看了 Apple 的示例,这似乎是矫枉过正。
我目前面临的问题主要是格式和颜色问题。功能似乎很好。我希望节标题有白色背景,行有蓝色背景。这是我的代码:
- (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 Bold,但它不在这里)
这是我在展开底部时得到的:
在我滚动 UITableView 以展开稍后的条目后,这种混乱开始发生......我想这与更新视图有关..
【问题讨论】:
-
我们看不到图片?你在使用 viewForSection 方法吗?
-
no.. 没有 viewForSection 方法。你指的是什么图片?
-
因为我看不到图片,请您告诉我发生了什么奇怪的事情?
-
我希望该部分具有白色背景,并且行没有披露按钮..我在代码中设置了它..但是你可以看到行有披露按钮
-
我编辑了上面的图片和问题,希望有点清楚
标签: iphone objective-c uitableview