美文网首页
iOS cell文本内容显示指定位置内容多行自动换行

iOS cell文本内容显示指定位置内容多行自动换行

作者: Lee坚武 | 来源:发表于2020-08-08 09:19 被阅读0次

// 换行符为\n,在需要换行的地方加上这个符号即可,

label.numberOfLines =0;

label.text = @"此处\n换行";
//计算行的高度.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellText = [self.array objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(240.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
}
//显示行.
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [self.array objectAtIndex:row];
//自动换行,这里最重要
cell.textLabel.numberOfLines = 0;
//Cell中的小箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

其中array为NSObject* 类型的存放NSString集合的对象。

完成代码如下:

// 设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    AdaptiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
//    // 传递数组模型
//    cell.backgroundColor = [UIColor  clearColor];
//    cell.model = self.dataSource[indexPath.row];
//    NSLog(@"%@传递数组模型======",cell.model);
//    return cell;
    // 先从缓存中找,如果没有再创建
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.dataSource[indexPath.row];
    //自动换行,这里最重要
    cell.textLabel.numberOfLines = 0;
    //Cell中的小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = fontNormal;
    return cell;
}
NSDictionary *serverDataDict = @{@"game_id":[NSNumber numberWithInt:[gameID intValue]]};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:gameMix parameters:serverDataDict progress:nil
                    success:^(NSURLSessionTask *task, id responseObject) {
        // responseObject已经被解析成了OC
        NSDictionary *dict = responseObject;
        NSLog(@"%@打印的字典=====",dict);
        NSMutableArray *arr = [NSMutableArray array];
        NSArray *temp = dict[@"data"];
        
        for (NSInteger i=0; i < temp.count; i++) {
            [arr addObject: [NSString stringWithFormat:@"%zd %@\n  %@",(i + 1), temp[i][@"title"], temp[i][@"create_time"]]];
        }
        // 由于这个block是在主线程中执行的所以可以直接在这里刷新数据
        [weakSelf refreshDataSource:arr];
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

相关文章

网友评论

      本文标题:iOS cell文本内容显示指定位置内容多行自动换行

      本文链接:https://www.haomeiwen.com/subject/fxwcdktx.html