我在cellForRowAtIndexPath方法中設置UILabel,當用戶改變文本時,標籤應該改變。但即使不是零,也不會改變。不能訪問UILabel
@property UILabel *myLabel;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, textView.frame.size.width, 37)];
self.myLabel.text = @"Old Text";
}
- (void)textViewDidChange:(UITextView *)textView {
self.myLabel.textColor = [UIColor redColor];
self.myLabel.text = @"New Text";
}
我發現了一個解決方案,有些人在主線程中更改UILabel,但這對我不起作用。
dispatch_async(dispatch_get_main_queue(), ^{
// Do in main thread
});
您發佈的代碼可能有效。你的'cellForRowAtIndexPath'不會出隊或創建新的單元格,也不會返回單元格。它不會將您創建的標籤附加到單元格。如果需要幫助,請發佈整個'cellForRowAtIndexPath'方法以及有關正在使用的'self.myLabel'屬性的信息。 –
你沒有創建一個新的單元格。每次調用cellForRowAtIndexPath時,都會再次初始化您的標籤,並再次分配「舊文本」。 –