我在cellForRowAtIndexPath方法中设置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
});
答案 0 :(得分:0)
如果您想要“动态”修改屏幕上的单元格的标签,那么您需要一种方法来获取标签。
我通常做的是在IB中为单元格创建模板,定义UITableViewCell
的自定义子类,并将单元格的类设置为该自定义子类。然后我在单元格和我创建的所有自定义字段之间连接出口。
在我的cellForRowAtIndexPath
方法中,我将单元格出列并将其强制转换为自定义类型。然后我通过单元格引用单元格中的字段,例如
cell.myLabel
。
如果您在cellForRowAtIndexPath
中向单元格添加新字段,则会遇到一些问题。首先,您需要确保在回收单元格时不添加该新字段的另一个副本。其次,如果你想修改它,你需要一种到达现场的方法,就像你的问题一样。有办法解决这些问题,但我上面概述的方法更清晰。
答案 1 :(得分:0)
Duncan,对不起,这是cellForRowAtIndexPath的其余部分。我认为这不是重复使用单元格的问题,因为我没有滚动所以不应该重新创建单元格。
@property NSString *textField_1;
@property NSString *textField_2;
// ...
@property UILabel *myLabel;
@end
@implementation TableViewController
- (void)textViewDidChange:(UITextView *)textView
{
self.myLabel.textColor = [UIColor redColor];
self.myLabel.text = @"New Text";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.section == 0)
{
float displayResolutionWidth = self.view.frame.size.width;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, displayResolutionWidth - 165, 50)];
textField.delegate = self;
textField.tag = indexPath.row;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, displayResolutionWidth - 90, 176)];
textView.delegate = self;
textView.tag = indexPath.row;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, textView.frame.size.width, 37)];
self.myLabel.textColor = [UIColor redColor];
if(indexPath.row == 17)
{
cell.accessoryView = textView;
[textView addSubview:self.myLabel];
}
else if (indexPath.row != 17)
{
cell.accessoryView = textField;
}
if (self.object)
{
if (indexPath.row == 0)
textField.text = self.object.property_1;
if (indexPath.row == 1)
textField.text = self.object.property_2;
}
else if (!self.object)
{
if (indexPath.row == 0)
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Placeholder_1", @"")
attributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];
if (indexPath.row == 1)
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Placeholder_2", @"")
attributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];
if (indexPath.row == 17)
self.myLabel.text = "Old Text";
}
if (indexPath.row != 17)
{
cell.textLabel.text = [self.arrayWithObjectData objectAtIndex:indexPath.row];
}
else if (indexPath.row == 17)
{
UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 150, 40)];
mylabel.text = self.arrayWithObjectData[17];
mylabel.textColor = [UIColor COLOR_TEXT];
[cell.contentView addSubview:mylabel];
}
}
return cell;
}
@end