滚动后UITableViewCell背景图像消失

时间:2013-08-11 08:56:14

标签: iphone ios objective-c ipad

我正在初始化EventEditViewController以使用以下代码添加新事件:

- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore
{
    EKEventEditViewController* vc = [[EKEventEditViewController alloc] init];
    vc.eventStore = eventStore;
    vc.delegate = self; // Probably self


    EKEvent* event = [EKEvent eventWithEventStore:eventStore];
    event.title = @"";
    event.startDate = [NSDate date];
    event.endDate = [NSDate date];
    event.URL = [NSURL URLWithString:@""];
    event.notes = @"";
    event.allDay = NO;
    vc.event = event;

    vc.editViewDelegate = self;

    [self presentViewController:vc animated:NO completion:nil];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UITableView *tableView = ((UITableViewController *)viewController).tableView;

    if ([viewController isKindOfClass:[UITableViewController class]]) {
        //((UITableViewController *)viewController).tableView.backgroundColor = [UIColor clearColor];
        for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
        {
            for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
            {
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath     indexPathForRow:i inSection:j]];

                    cell.backgroundView =  [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background-1.png"]];
                    cell.textLabel.textColor = [UIColor whiteColor];
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                }
            }
    }
 }

UITableView按原样显示,但是当我开始滚动直到UITableViewCell不再出现在iPhone的屏幕上然后向后滚动时,TableViewCell会忘记它的图像并变成标准{{ 1}}(没有任何背景图片或自定义)。

滚动前的屏幕截图:http://postimg.org/image/vzlj3t5o9/

滚动后的

屏幕截图:( http://postimg.org/image/c30836v2v/

如何解决此问题?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

由于某些原因,您不应该尝试在tableView:cellForRowAtIndexPath:实施的上下文之外管理表格单元格的外观。但由于您无法对EKEventEditViewController工作流程中涉及的各种视图控制器进行子类化,因此您无法以通常的方式编写这些方法的实现。但是,您可以使用Objective-C的一些反射和运行时插入您自己的UITableViewDataSource实现。

基本思想是使用您控制的一个来切换表视图的数据源,但仍然依赖于Event Kit UI框架提供的实现,并且只对他们返回的单元格应用可视调整。以下是FakeTableViewDataSource

的界面和实现
@interface FakeTableViewDataSource : NSObject <UITableViewDataSource>

@property (nonatomic) id<UITableViewDataSource> realDataSource;

@end

@implementation FakeTableViewDataSource


- (BOOL)respondsToSelector:(SEL)aSelector
{
    BOOL responds = [super respondsToSelector:aSelector];
    if (!responds && self.realDataSource) {
        responds = [self.realDataSource respondsToSelector:aSelector];
    }

    return responds;
}


- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    if ([self.realDataSource respondsToSelector:[anInvocation selector]]) {
        [anInvocation invokeWithTarget:self.realDataSource];
    }
    else {
        [super forwardInvocation:anInvocation];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.realDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor greenColor];
    return cell;
}

@end

您实施tableView:cellForRowAtIndexPath:来实施应用程序的特定视觉调整,我选择了文本标签颜色,因为它显然是否有效。

现在,在您的UINavigationControllerDelegate方法中,不是迭代单元格,而是替换数据源。我的实现如下所示:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([viewController isKindOfClass:[UITableViewController class]]) {
        UITableView *tableView = ((UITableViewController *)viewController).tableView;
        if (![tableView.dataSource isKindOfClass:[FakeTableViewDataSource class]]) {
            FakeTableViewDataSource *fakeDataSource = [[FakeTableViewDataSource alloc] init];
            fakeDataSource.realDataSource = tableView.dataSource;
            tableView.dataSource = fakeDataSource;
            [self.fakeDataSources addObject:fakeDataSource];
        }
    }
}

应该说,从可维护性的角度和应用程序商店批准的角度来看,以这种方式篡改框架具有潜在的风险。所以在使用这样的技术时要小心。

相关问题