insertRowsAtIndexPaths自定义样式

时间:2014-07-21 05:25:05

标签: ios iphone ios7

我正在尝试创建像此collapse table

这样的折叠表

我的代码:

- (WordListTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"wordListTableCell";

    WordListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[WordListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }


    NSDictionary *uu = [wordList objectAtIndex:indexPath.row];

    if(!addingNewCellFlag)
    {
        if([[[Singleton sharedInstance] currentLaguage] isEqualToString:@"en"])
        {
            cell.textLabel.text = [uu valueForKey:@"tiitle_en"];
        }
        else if([[[Singleton sharedInstance] currentLaguage] isEqualToString:@"ru"])
        {
            cell.textLabel.text = [uu valueForKey:@"tiitle_ru"];
        }
        UIImage *buttonImage;
        if(indexPath.row==0)
        {
            buttonImage = [[UIImage imageNamed:@"rowFirst.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        }
        else
        {
            buttonImage = [UIImage imageNamed:@"rowMiddle.png"];
        }

        cell.backgroundView = [[UIImageView alloc] initWithImage:buttonImage];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.isCanExpanded = TRUE;
    }
    else
    {
        if([[[Singleton sharedInstance] currentLaguage] isEqualToString:@"en"]){
            cell.textLabel.text = [uu valueForKey:@"content_en"];
        }
        else if([[[Singleton sharedInstance] currentLaguage] isEqualToString:@"ru"])
        {
            cell.textLabel.text = [uu valueForKey:@"content_ru"];
        }
        cell.backgroundColor = [UIColor colorWithRed:253/255.0 green:252/255.0 blue:222/255.0 alpha:1];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.textAlignment = NSTextAlignmentJustified;
        cell.isCanExpanded = FALSE;
    }
    [cell setIsExpanded:FALSE];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WordListTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    addingNewCellFlag = TRUE;
    if(cell.isCanExpanded)
    {
        if(![cell isExpanded]){

            [cell setIsExpanded:TRUE];

            NSDictionary *uu = [wordList objectAtIndex:indexPath.row];
            [wordList insertObject:uu atIndex:indexPath.row+1];


            [self.tableView beginUpdates];

            [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]
                                  withRowAnimation:UITableViewRowAnimationTop];
            [self.tableView endUpdates];

        }
         else if ([cell isExpanded])
        {
            [cell setIsExpanded:FALSE];
            [wordList removeObjectAtIndex:indexPath.row+1];
            [self.tableView beginUpdates];
            NSIndexPath * nextIndexPath = [NSIndexPath  indexPathForRow:indexPath.row+1 inSection:indexPath.section];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationTop];

            [self.tableView endUpdates];
        }
    }
    cell = nil;

}

它有效,如果我们点击单元格我们创建破坏单元格,新单元格是否有正确的文本([uu valueForKey:@"content_en"]),但有错误的backgroundColor(cell.backgroundView = [[UIImageView alloc] initWithImage:buttonImage];)。但有时候backgroundColor是正确的([UIColor colorWithRed:253/255.0 green:252/255.0 blue:222/255.0 alpha:1])。

请告诉我,这种方法是如何运作的。

1 个答案:

答案 0 :(得分:0)

我在

中看不到你的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

当你处理不同索引路径的单元格时,可能会出错。