ios - 在 UITableViewCell 中为 textLabel 设置边界

标签 ios objective-c uitableview

我试图用一个问题填充我的每个 UITableViewCells,每个问题都在一个 NSArray 中。这些问题比 iPhone 屏幕可以显示的更宽,我无法弄清楚如何显示问题,因此它返回到下一行以显示整个问题。

我最初使用继承了 UITableViewCell 的 CustomCell 类,并尝试将每个问题显示为 textView,但我根本无法显示我的问题。

我的目标是为 UITableView 的每个部分提供 3 行,这些行都将由一个按钮填充,供用户回答每个问题。

enter image description here

这是我必须填充 UITableView 的代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [survey.questions count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

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

    static NSString *simpleTableIdentifier = @"MyCustomCell";
    /*
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    cell.textLabel.text = que;
    cell.textLabel.numberOfLines = 0;
    */

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }


    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];

    NSLog(@"Que Value: %@\n", que);
    cell.queLabel.text = que;
    cell.queLabel.numberOfLines = 0;


    /*
    if (numberOfSectionsInTableView.indexPath.section == 1) {
        NSLog(@"Button 1");
    }
    */

    return cell;
}

最佳答案

您可以使用以下命令设置自动换行:

cell.textLabel.numberOfLines = 0;

如果您想向 Storyboard 中的单元格添加按钮,请创建一个新的 .xib 文件并以这种方式布置单元格。

关于ios - 在 UITableViewCell 中为 textLabel 设置边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188854/

相关文章:

ios - 如何在带有 IB 的 iOS 中使用自动布局创建带有头像和文本的 UITableView Cell?

ios - 如何在Tesseract OCR中设置书写方向?

objective-c - 单击自定义删除按钮删除 UITableViewCell

ios - 自定义 tableview Controller 的单元格文本

iphone - UITableView 部分标题绘制在前 subview 上方

ios - reloadRowsAtIndexPaths 使用 JSON imageUrls 崩溃

ios - UITableViewCell 图像加载错误的图像

iOS Compass 与独立开发者版本

iphone - 将 NSNumber 转换为 BOOL Objective-C

ios - Collection View 布局为零,我已经尝试了一切