【问题标题】:Accessing cell attributes outside of cellForRowAtIndexPath访问 cellForRowAtIndexPath 之外的单元格属性
【发布时间】:2013-04-19 16:52:51
【问题描述】:

我在注册控制器上设置了五个字段。用户名、显示名、密码、确认密码和电子邮件地址。

它们的设置如下:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = [UIColor colorWithRed:14.0f/255.0f green:62.0f/255.0f blue:178.0f/255.0f alpha:0.8f];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0f];


    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = NSLocalizedString(@"UsernameFieldLabel", @"Username field label");
            [cell addSubview:self.usernameField];
            break ;
        }
        case 1: {
            cell.textLabel.text = NSLocalizedString(@"DisplayNameFieldLabel", @"Displayname field lael");
            [cell addSubview:self.displayNameField];
            break ;
        }
        case 2: {
            cell.textLabel.text = NSLocalizedString(@"PasswordFieldLabel", @"Password field lael");
            [cell addSubview:self.passwordField];
            break ;
        }
        case 3: {
            cell.textLabel.text = NSLocalizedString(@"ConfirmPasswordFieldLabel", @"Confirm Password field lael");
            cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines = 2;
            [cell addSubview:self.confirmPasswordField];
            break ;
        }
        case 4: {
            cell.textLabel.text = NSLocalizedString(@"EmailFieldLabel", @"Email field lael");
            [cell addSubview:self.emailField];
            break ;
        }
    }

    return cell;

细胞本身工作正常。然后我在一个按钮上进行了一些验证,用于检查有效的电子邮件地址等,这也可以正常工作。

我想做的是更新验证代码中的 cell.textLabel.textColor 属性。例如,如果电子邮件地址无效,我想将标签文本颜色更新为红色以使其突出(我还对响应者进行排序等)。

如何在单元格设置之外获得对这个特定单元格的引用?

编辑 这是答案之后的变化。我需要访问第 1 部分中的第 5 个 cll(索引路径 4)(表格视图中只有一个部分):

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:1];

UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor redColor];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

【问题讨论】:

  • 更改 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:1]; as NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0];

标签: ios ios5 uitableview custom-cell


【解决方案1】:

更新了 Swift,

let cell = tableView.cellForRow(at: indexPath) as! <Insert tableviewcell Controller here>

然后您可以像在cellforrowat 中一样访问单元格内的所有对象

let displayname = cell.labelfordisplayname.text!
print("This is the displayname -> \(displayname)")

【讨论】:

    【解决方案2】:

    在 UiTableView "didselectRow" 中访问自定义标签

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = (UILabel*)[cell viewWithTag:101]; // you can get label like
    label.backgroundColor=[UIColor redColor];
    

    从按钮获取索引路径

    - (void) cellEditAction:(UIButton *)sender {
    
      CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:FriendsTable];
    NSIndexPath *indexPath = [FriendsTable indexPathForRowAtPoint:buttonPosition];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = (UILabel*)[cell viewWithTag:101]; // you can get label like
    label.backgroundColor=[UIColor redColor];
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用标签创建。

      在 cellForRowAtIndexPath 中

      static NSString *cellId = @"Cell";
      CustomCell *cell = (CustomCell *)[self.tblView dequeueReusableCellWithIdentifier:cellId];
      if (cell == nil) {
          NSArray * myNib;
          myNib =[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
          cell = (CustomCell *)[myNib lastObject];
      }
      [cell.deleteBtn addTarget:self action:@selector(cellDeleteAction:) forControlEvents:UIControlEventTouchUpInside];
      cell.deleteBtn.tag = indexPath.row;
      [cell.editBtn addTarget:self action:@selector(cellEditAction:) forControlEvents:UIControlEventTouchUpInside];
      cell.editBtn.tag = indexPath.row;
      

      之后你可以像这样创建按钮操作方法,

      - (void) cellEditAction:(UIButton *)sender {
      UIButton *button = (UIButton *)sender;
      NSString *rateId = [[resDict valueForKey:@"value"]objectAtIndex:button.tag];
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用 NSIndexPath 获取 UITableViewCell。

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:YOUR_ROW inSection:YOUR_SECTION];
        
        UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath];
        cell.textLabel.textColor = YOUR_COLOR;
        [yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        

        希望对你有帮助。

        【讨论】:

        • 最后一行出现 SIGBRT 错误:'NSInternalInconsistencyException',原因:'尝试从第 1 部分删除第 4 行,但更新前只有 1 个部分'
        • 我已经用我根据您的回答使用的代码更新了问题,但生成了这个没有意义的错误。
        • 您的表格中有多少个部分?如果只有一个部分,则将部分传递为 0 而不是 1。
        • 如果是自定义单元格,我们怎么做?你有什么想法吗
        【解决方案5】:

        只需致电[self tableView:self.tableView cellForRowAtIndexPath:indexPath]。它将返回给定索引路径的单元格。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-02
          • 1970-01-01
          • 1970-01-01
          • 2010-10-20
          • 1970-01-01
          • 2016-03-13
          • 1970-01-01
          相关资源
          最近更新 更多