您好,我在做某事时遇到麻烦。我有一个带有部分的静态TableView,并且试图将我的文本放置在某个位置,因此我将ui标签链接到uitableviewcell。但是由于某种原因,它不起作用。如果您对我为什么遇到这个问题有任何想法,那将很棒。我对iOS开发还是有点陌生​​,只是把它放在那里。

TableViewController.m:

}

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

    static NSString *CellIdentifier = @"TableCell";
    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    int row = [indexPath row];

    if (indexPath.section==0)
        cell.textLabel.text = _1[row];
    if (indexPath.section==1)
        cell.textLabel.text = _2[row];
    if (indexPath.section==2)
        cell.textLabel.text = _3[row];
    if (indexPath.section==3)
        cell.textLabel.text = _4[row];
    if (indexPath.section==4)
        cell.textLabel.text = _5[row];
    if (indexPath.section==5)
        cell.textLabel.text = _6[row];
    if (indexPath.section==6)
        cell.textLabel.text = _7[row];
    if (indexPath.section==7)
        cell.textLabel.text = _8[row];
    return cell;
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

TableCell.h:
#import <UIKit/UIKit.h>

@interface TableCell : UITableViewCell
@property (strong,nonatomic) IBOutlet UILabel *TitleLabel;
@end

最佳答案

替换这条线

cell.textLabel.text = _1[row];


cell.TitleLabel.text = _1[row];

这条线不再需要
if (cell == nil) {
    cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

如果TableCell与xib一起使用,则需要加载它。
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    // Load the top-level objects from the custom cell XIB.
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];
}
cell.TitleLabel.text = _1[row];

return cell;

你应该检查一下
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

告诉表视图如何创建新单元格的方法。如果指定类型的单元格当前不在重用队列中,则表视图将使用提供的信息自动创建新的单元格对象。如果您先前使用相同的重用标识符注册了一个类或nib文件,则在cellClass参数中指定的类将替换旧条目(Apple Document)

在您的viewDidLoad方法中
[self.tableView registerClass:[TableCell class]
       forCellReuseIdentifier:@"CellIdentifier"];

关于ios - UILabel未显示在静态TableView上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21655947/

10-10 20:53
查看更多