Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更改UITableView中静态单元格中的textlabel_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 更改UITableView中静态单元格中的textlabel

Ios 更改UITableView中静态单元格中的textlabel,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个静态UITableView,它是我在故事板中创建的每个单元格的内容,但我需要在运行时以编程方式更改某些单元格的标签。如何操作?为要在表视图控制器中更改的每个单元格创建一个属性,如下所示: @property (weak) IBOutlet UITableViewCell *cell1; @property (weak) IBOutlet UITableViewCell *cell2; 将每个单元连接到Interface Builder中的一个单元 当您只需要更改标签的文本时,可以使用

我有一个静态UITableView,它是我在故事板中创建的每个单元格的内容,但我需要在运行时以编程方式更改某些单元格的标签。如何操作?

为要在表视图控制器中更改的每个单元格创建一个属性,如下所示:

@property (weak) IBOutlet UITableViewCell *cell1;
@property (weak) IBOutlet UITableViewCell *cell2;
将每个单元连接到Interface Builder中的一个单元

当您只需要更改标签的文本时,可以使用

self.cell1.textLabel.text = @"New Text";
如果需要更换整个标签,请使用

UILabel *newLabel = [[UILabel alloc] init];
self.cell2.textLabel = newLabel;

您可以使用cellforrowatinexpath方法从表视图获取单元格,您需要定义一个用于检索表视图的出口

__weak IBOutlet UITableView *tableView;
之后,您可以获得如下单元格:

NSIndexPath* indexPath = [NSIndexPath indexPathForRow:yourRow inSection:yourSection];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[[cell textLabel] setText:@"Your new text"];
也许在设置文本后,您需要调整标签或单元格高度,如果您需要更深入的帮助,请提供更多信息,我很乐意为您提供帮助


你已经完成了,希望能有所帮助。

@rospenman给出了一个很好的答案

@ggrana指出了一个与内存和单元重用有关的潜在问题,但是不要担心这个问题

对于带有静态单元格的
UITableView
,在对
UITableViewController
调用
viewDidLoad
之前,所有单元格都预先实例化,并且不会像动态单元格那样重复使用。因此,您甚至可以将
IBOutlets
直接带到
UITextFields
UISwitches
UILabels
以及您真正感兴趣的内容,这些内容都放在故事板的静态单元格中。

我需要这些

dispatch_async(dispatch_get_main_queue(), ^{
    (...textLabel updates)
    [self.tableView reloadData];
});

你可以这样做

for (int section = 0; section < [table numberOfSections]; section++) {
    for (int row = 0; row < [table numberOfRowsInSection:section]; row++) {
        NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell = [self cellForRowAtIndexPath:cellPath];
        cell.backgroundColor =  [UIColor blackColor];
        cell.textLabel.textColor = [UIColor whiteColor;
        cell.textLabel.text = @"Your text";
    }
}
for(int section=0;section<[table numberOfSections];section++){
对于(int row=0;row<[表号rowsinssection:section];row++){
nsindepath*cellPath=[nsindepath indexPathForRow:row-instation:section];
UITableViewCell*单元格=[self-cellForRowAtIndexPath:cellPath];
cell.backgroundColor=[UIColor blackColor];
cell.textLabel.textColor=[UIColor whiteColor;
cell.textlab.text=@“您的文本”;
}
}
另外,请确保您的更改不显示在ViewDidDisplay中,并将其放置在ViewWillDisplay中,否则您将遇到问题

希望它能起作用:

@IBOutlet weak var yourTextField: UITextField!
private var yourText: String?
我只是在此tableview的委托中自定义:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    guard let yourText = yourText else { return }
    yourTextField.text = yourText
}
更改文本时:

yourText = "New text here"
tableView.reloadData()
Swift解决方案:

首先制作一个静态TableViewCell的IBOutlet

@IBOutlet weak var cellFirst: UITableViewCell!
然后在viewDidLoad中更改标签名称

cellFirst.textLabel?.text = "Language"

注意:如果您在TableViewCell上附加了标签,则只需使用情节提要将其隐藏即可。

您可以尝试它。

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{
    cell.textLabel?.text = titles[indexPath.row]
}

不,您不能保留所有单元格,因为您必须重复使用单元格,并且不能将所有单元格加载到内存中,您必须只加载显示的单元格!@ggrana由于这些是我们正在处理的静态单元格,它们不太可能有那么多,因此内存使用量不会太大。不过,我同意您的看法你编辑的答案比我的好,所以我对它进行了投票。你不需要声明tableView的出口,因为你已经在使用UITableViewController。配置了“静态单元格”的UITableView只能存在于UITableViewController的上下文中。我不知道你可以从静态单元格创建
IBOutlets
。你必须挖掘它我想,你是在研究文档吧。