IOS在UITableView中点击UILabel

时间:2014-07-03 07:52:50

标签: ios uitableview uiview

我正在尝试创建一个类似于Facebook主页视图的视图,该视图具有多个可点击区域。

一切都已完成(虽然我不知道我是否遵循了这个atm的正确方法)但视图的对齐方式不正确。数据以enter image description here的形式出现在不同的行上。我真正想要实现的是enter image description here

我正在UITableView显示我的数据。

我尝试过这段代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
RequestInfo *requestInfo = [self.myDataArray objectAtIndex:indexPath.row];

NSArray *localizedStringPieces = [requestInfo.feed componentsSeparatedByString:@"#"];
NSUInteger msgChunkCount = localizedStringPieces ? localizedStringPieces.count : 0;

CGPoint wordLocation = CGPointMake(0.0, 0.0);
for (NSUInteger i = 0; i < msgChunkCount; i++)
{

    NSString *chunk = [localizedStringPieces objectAtIndex:i];

    if ([chunk isEqualToString:@""])
    {
        continue;
    }
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:16.0f];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByCharWrapping;

    label.preferredMaxLayoutWidth = cell.cellView.frame.size.width;
    CGSize maximumLabelSize = CGSizeMake(280, FLT_MAX);

    CGSize expectedLabelSize = [chunk sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:label.lineBreakMode];



    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
        if ([[identifireArray objectAtIndex:indexPath.row] count]> 0) {

        for (int i = 0; i < [[identifireArray objectAtIndex:indexPath.row] count]; i++) {
            if ([chunk hasPrefix:[NSString stringWithFormat:@"(%@)",[[identifireArray objectAtIndex:indexPath.row] objectAtIndex:i]]]) {
                label.text = chunk;
                label.textColor = [UIColor colorWithRed:110/255.0f green:181/255.0f blue:229/255.0f alpha:1.0];
                label.highlightedTextColor = [UIColor yellowColor];
                label.userInteractionEnabled = YES;
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
                [label addGestureRecognizer:tapGesture];
                label.text = [label.text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"(%@)",[[identifireArray objectAtIndex:indexPath.row] objectAtIndex:i]] withString:@""];


                break;
            } else {
                label.textColor = [UIColor blackColor];
                label.text = chunk;
                label.userInteractionEnabled = NO;
             }
        }
    }

    [label sizeToFit];

    if (cell.cellView.frame.size.width < wordLocation.x + label.bounds.size.width)
    {
        wordLocation.x = 0.0;                       // move this word all the way to the left...
        wordLocation.y += label.frame.size.height;  // ...on the next line


        NSRange startingWhiteSpaceRange = [label.text rangeOfString:@"^\\s*"
                                                            options:NSRegularExpressionSearch];
        if (startingWhiteSpaceRange.location == 0)
        {
            label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange
                                                             withString:@""];
            [label sizeToFit];
        }
    }

    // Set the location for this label:
    label.frame = CGRectMake(wordLocation.x,
                             wordLocation.y,
                             label.frame.size.width,
                             label.frame.size.height);



    [cell.cellView addSubview:label];

    // Update the horizontal position for the next word:
    wordLocation.x += label.frame.size.width;



};


return cell;

}

以下是我使用的示例源文本:

#(team)Team_Football_Test# won the football match #(event)testing football# against #(team)Bengal Tigers Football Club(DG Testing)#
#(team)Team_crick_Test# won the football match #(event)C4 cricket# against #(team)Bengal Tigers Cricket Club(DG Testing)#

当我拆分字符串并将其添加到UILabel时,我无法正确对齐它。我希望看起来像Facebook墙页一样,所有数据都正确对齐,但我的视图不对齐。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

请勿使用多个标签,也不要在tableView:cellForRowAtIndexPath:中创建新标签并添加为子视图。

因此,首先,创建一个自定义单元格子类,它会添加您的标签和任何其他视图,并将视图放在正确的位置(并调整它们的大小)。

对于标签内容,请使用attributedText代替text,并使用“块”信息将属性格式应用于标签中的文本(而不是创建多个不同的标签)。

答案 1 :(得分:0)

我同意Wain。

创建自定义单元格并添加所有标签,图像和任何默认格式。

用户界面构建器以可视方式创建所有这些。这将使您的生活更轻松。