我正在尝试按照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以扩展后来的条目之后,这个混乱开始发生...我猜它与更新视图有关..
答案 0 :(得分:0)
要在行外观中进行更改,您应该在cellForRowAtIndexPath
方法中更改它们。对该方法做出适当的改变。
更改tableView的标题,您可以使用以下代码...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[tempView setBackgroundColor:[UIColor whiteColor]];
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 44)];
[tempLabel setBackgroundColor:[UIColor clearColor]];
[tempLabel setTextColor:[UIColor blackColor]];
[tempLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
[tempLabel setText:[sectionTitles objectAtIndex:section]];
[tempView addSubview:tempLabel];
[tempLabel release];
return [tempView autorelease];
}
谢谢,
答案 1 :(得分:0)
细胞重用问题。当您重复使用表格单元格时,每次都需要重置/设置每个单元格属性。
例如,cell.accessoryView是上一次使用该单元格时发生的任何事情。尝试将其设置为零。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Creating NEW cell
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Now you are using either NEW or OLD cell
// You MUST set each cell property that can be different
// code to customize cell for certain purpose
return cell;
}
根据部分和行,您似乎设置了cell.textLabel.font,cell.textLabel.text,cell.accessoryView,cell.contentView.backgroundColor,cel.textLabel.backgroundColor等。
细胞重用意味着有时候你会得到一个旧的细胞,就像上次使用的那样。因此,您需要设置之前设置的所有值。如果某些值始终相同,则在创建分支新单元格时仅定义一次。
是的,这是很多工作。您可以更改所有那些long if子句来切换案例,开始使用通过索引访问的预定义数组,也可以使用Interface Builder XIB来处理不同的单元格。如果您愿意,您还可以为不同类型的单元格使用多个不同的cellIdentifier。在这种情况下,重用问题较少,但更容易犯错误......