gpt4 book ai didi

IOS 在 UITableView 中点击 UILabel

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:04 25 4
gpt4 key购买 nike

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

一切都已完成(虽然我不知道我是否遵循了这个 atm 的正确方法)但是 View 的对齐方式不正确。数据以 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 墙页面一样,其中所有数据都正确对齐,但我的 View 未对齐。我该如何解决这个问题?

最佳答案

不要使用多个标签,也不要创建新标签并将其添加为 tableView:cellForRowAtIndexPath: 中的 subview 。

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

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

关于IOS 在 UITableView 中点击 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24548029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com