【问题标题】:Problem while scrolling the UITableView with custom cell that contains a UItextview使用包含 UItextview 的自定义单元格滚动 UITableView 时出现问题
【发布时间】:2011-07-06 11:37:13
【问题描述】:

我有一个包含 UITextView、UILabel 和 UIButton 的自定义单元格的表格视图。

问题是,

第一次加载表格时,会显示 2 个单元格并且没问题。但是在滚动表格时,任何单元格都会滚动出屏幕,应用程序突然终止,而没有向控制台显示任何异常错误。

这是我的代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSLog(@"In tableView:Cell");    
static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";

CourtsFavoriteCustomCell* cell = (CourtsFavoriteCustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

if( cell == nil)
    {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CourtsFavoriteCustomCell"
                                                    owner:self options:nil];

    for (id oneObject in nib) 
        if ([oneObject isKindOfClass:[CourtsFavoriteCustomCell class]])
        cell = (CourtsFavoriteCustomCell *)oneObject;

//*********** Creating button inside each TableView cell..

        UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;

        [mapButton setFrame:CGRectMake(120.0f, 157.0f, 72.0f, 32.0f)] ;
        //btnDetail.backgroundColor = [UIColor grayColor];
        [mapButton setTitle:@"Map" forState:UIControlStateNormal];
        [mapButton setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
        //[mapButton.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:12]];
        [mapButton setBackgroundColor:[UIColor clearColor]];
        [mapButton sizeThatFits:mapButton.frame.size];
        [cell addSubview:mapButton];
        [mapButton addTarget:self 
                      action:@selector(cellMapButtontapped:)
            forControlEvents:UIControlEventTouchUpInside];
        forState:UIControlStateNormal];

    }
NSLog(@"App get terminated after showing this log message to console when scrolling tableview");



    cell.name.text         = favCourtName;  // name is a UIText view and if comment this line of code, every thing works fine
    cell.address.text      = favCourtAddress;   


    cell.accessoryType          = UITableViewCellAccessoryDetailDisclosureButton; 

     return cell;


}

我在我的自定义单元格中使用 UITextView(代码中的 Cell.name)。当我尝试将 UITextView 代码注释掉时,一切正常。

请帮帮我 :)thanx..

【问题讨论】:

  • 什么是favCourtName?是否设置正确?
  • 我可以知道 uitextview 对象在哪里声明了相同的类或在哪里。属性很重要,所以如果将更改分配给保留
  • @deepak ,favCourtName 是一个 NSString,是的,它已正确设置..
  • @maheswaran , UItextView 在自定义单元格中声明。 @property (nonatomic, retain) UITextView * name;

标签: iphone objective-c uitableview uitextview


【解决方案1】:

来自 Apple 文档;

重要提示:您不应在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。如果这样做,可能会导致意外行为,因为这两个对象的触摸事件可能会混淆和错误处理。

UITableView 和 UITextView 都是 UIScrollView 的子类。

一旦你解决了你的记忆问题,你仍然会得到意想不到的行为。您可能应该重新考虑这种设计,以支持受支持的设计。

您没有提供足够的信息来提供进一步帮助。您在控制台上收到什么错误消息?

与您的问题无关:使用大写第一个字符(即cell.Name)命名属性是不典型的。单元格上的 Name 属性应为小写(即name)。

【讨论】:

  • 实际上我在代码中使用了 cell.name 。当我在这里输入(现已编辑)时,这是一个输入错误。如果在 UITableView 单元格中使用 UITextView 是不明智的,我还能用什么来显示冗长的字符串? -谢谢
  • 使用 UILabel 并省略字符串,详细信息视图控制器中的详细信息,您可以通过将详细视图控制器推到包含根视图控制器的导航控制器上吗?我无法从这个简短的交流中看出你应该如何设计应用程序的用户体验。但是你上面列出的内容是行不通的。
  • 非常感谢您的回答:)
【解决方案2】:

@All,谢谢你所有的答案.. :) 我解决了问题。不是 UITextView。

以及为什么我在这里写信是因为,如果其他人来到这里遇到同样的问题,它可能会有所帮助。

问题其实很简单,但我花了 2 天时间才发现,因为它没有显示任何异常。

我所做的是,只是在我的 ViewDidLoad 方法中将 'retain' 添加到我的 'favCourtName' 中。是这样的,

NSString * favCourtName = [[[NSString alloc] initWithFormat:@"myCourt"] retain];

“保留”这个词具有魔力。谢谢

【讨论】:

    【解决方案3】:

    favCourtName 在某处发布,是否只分配一次?还是在每个单元格上都会初始化?

    cell.Name.text         = favCourtName;
    

    关于性能的第二件事,如果你使用下面的代码,它会在滚动时变慢,因为它是从 Nib 文件加载的,你可以使用initWithStyle: 工作。

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CourtsFavoriteCustomCell"
                                                    owner:self options:nil];
    
    for (id oneObject in nib) 
        if ([oneObject isKindOfClass:[CourtsFavoriteCustomCell class]])
        cell = (CourtsFavoriteCustomCell *)oneObject;
    

    【讨论】:

    • 我的猜测 - 名称是在 nib 文件中设置的插座。样式名称已(或应该)在 nib 文件中设置。
    • @Jignesh Brahmkhatri ,favCourtName 是一个 NSString。 cell.name 中的名称是我的自定义单元格类中的 UITextView。它的属性是这样的 @property (nonatomic, retain) UITextView * name;为什么我从笔尖加载是因为它是定制设计。我可以使用什么替代方法来使其快速。 -谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多