Accessing cell attributes outside of cellForRowAtIndexPath

折月煮酒 提交于 2019-12-01 16:32:13

问题


I have five fields setup on a Signup controller. Username, displayname, password, confirm password and email address.

They are setup with the following:

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;

The cells themselves work fine. I then have some validation on a button which checks for a valid email address etc, this also works fine.

What I would like to be able to do is update the cell.textLabel.textColor attribute in the validation code. For example, if the email address is not valid I want to update the label text color to Red so that it stands out (I also sort the responder etc).

How do I get a reference to this specific cell outside of the setup of cells?

EDIT This is the change following an answer. I need to access the 5th cll (index path 4) in section 1(only one section in table view):

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];

回答1:


you can get UITableViewCell using NSIndexPath.

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];

Hope it helps you.




回答2:


Just call [self tableView:self.tableView cellForRowAtIndexPath:indexPath]. It will return the cell at the given index path.




回答3:


Access Custom Label In UiTableView "didselectRow"

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

Get index path from button

- (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];

}



回答4:


You can create by using tags.

in 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;

after that you can create button action method like this,

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



回答5:


Updated Swift,

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

Then you can access all the objects inside your cell the same way as when you are in cellforrowat

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


来源:https://stackoverflow.com/questions/16220777/accessing-cell-attributes-outside-of-cellforrowatindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!