【问题标题】:Textview in tableviewcell getting overwrittentableviewcell中的Textview被覆盖
【发布时间】:2014-06-02 14:49:12
【问题描述】:

我有一个表格视图,我想在其中填充一些推文。我有一个文本视图,我使用它在每个表格视图单元格中填充推文的内容。但是,此文本视图已被覆盖。有人可以看看我下面的代码,如果我犯了任何错误,请告诉我?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FeedCell" forIndexPath:indexPath];

    NSDictionary *tweet = tweetArray[indexPath.row];

    NSString *tweetText = tweet[@"text"];

    CGSize stringSize = [tweetText sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];

    textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+5)];

    textV.font = [UIFont systemFontOfSize:15.0];

    textV.text = tweetText;

    textV.textColor = [UIColor blackColor];
    textV.editable = NO;

    [cell.contentView addSubview:textV];

    return cell;
}

【问题讨论】:

    标签: ios7 uitableview uitextview


    【解决方案1】:

    您的推文很少,因此不要去队列(即重复使用单元格),而是创建新单元格。使用以下代码:

    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:nil];        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        }
    NSDictionary *tweet = tweetArray[indexPath.row];
    
    NSString *tweetText = tweet[@"text"];
    
    CGSize stringSize = [tweetText sizeWithFont:[UIFont boldSystemFontOfSize:15]     constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
    
    textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+5)];
    
    textV.font = [UIFont systemFontOfSize:15.0];
    
    textV.text = tweetText;
    
    textV.textColor = [UIColor blackColor]; textV.editable = NO;
    
    [cell.contentView addSubview:textV];
    
            return cell; 
    

    }

    希望对你有帮助。

    【讨论】:

    • 如果我有 20 条推文,可以创建一个新单元格而不是出列它吗?
    • 是的,没关系,您最好重用不在视图中的单元格,而不是创建。你在这里使用故事板吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多