首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >insertRowsAtIndexPaths自定义样式

insertRowsAtIndexPaths自定义样式
EN

Stack Overflow用户
提问于 2014-07-21 13:25:05
回答 1查看 247关注 0票数 0

我正在尝试创建一个像这样的折叠表

我的代码:

代码语言:javascript
复制
- (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])。

请告诉我,这个方法是如何工作的。

EN

回答 1

Stack Overflow用户

发布于 2014-07-21 16:19:43

我没有看到你的代码在

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24858199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档