Reputation: 20670
I am creating custom cells and updating some UILabel
s every time when that view appears.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lblFacingValue = nil;
static NSUInteger const kFacingLabelTag = 7;
CGFloat LeftLabelWidth = 130.0;
static NSString *CellIdentifier = @"Cell";
ClientState *clientState = [ClientState sharedInstance];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.row == 0)
{
lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblFacingValue.font = [UIFont systemFontOfSize:21];
lblFacingValue.textAlignment = UITextAlignmentLeft;
lblFacingValue.backgroundColor = [UIColor clearColor];
lblFacingValue.tag = kFacingLabelTag;
lblFacingValue.textColor = [UIColor yellowColor];
[cell.contentView addSubview:lblFacingValue];
}
}
lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction];
}
But the problem is that it contains the value which I assign first time and do not update it when I open that view. that view is in tab.
It did not update the value of lblFacingValue and skip the check
if (cell == nil)
after first time. but do not update lblFacingValue.
Upvotes: 0
Views: 1344
Reputation: 241
Azhar, I think you forget to set the lblFacingValue variable to the actual UILabel object when the call to dequeueReusableCellWithIdentifier does not return nil. You should add:
lblFacingValue = [cell viewWithTag:kFacingLabelTag];
before calling
lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction];
Hope this helps!
Upvotes: 3
Reputation: 5681
Keep one thing in mind, when cell is nil, you construct the cell. Don't assign values there. Just create subviews etc.
This code should fix it. Also see my comments in it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSUInteger const kFacingLabelTag = 7; //This should rather be a #define ..
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.row == 0)
{
CGFloat LeftLabelWidth = 130.0;
UILabel *lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblFacingValue.font = [UIFont systemFontOfSize:21];
lblFacingValue.textAlignment = UITextAlignmentLeft;
lblFacingValue.backgroundColor = [UIColor clearColor];
lblFacingValue.tag = kFacingLabelTag;
lblFacingValue.textColor = [UIColor yellowColor];
[cell.contentView addSubview:lblFacingValue];
}
}
//now, we might be reusing the cell, so in either case, pick the label
//and edit the value.
ClientState *clientState = [ClientState sharedInstance];
UILabel *theLabel = (UILabel*) [cell.contentView viewForTag:kFacingLabelTag];
theLabel.text = [NSString stringWithFormat:@"%@", clientState.Direction];
}
Upvotes: 2
Reputation: 16827
The problem is that your specially configured cell has the same cell identifier as the others,
static NSString *CellIdentifier = @"Cell";
So the system doesn't differentiate when dequeuing cells between your special cell and normal cells. You should create a different cell identifier for the special cell, and dequeue using that when indexPath.row == 0;
Upvotes: 0
Reputation: 69469
You forget a piece where you get the label by it tag if you do dequeue a cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lblFacingValue = nil;
static NSUInteger const kFacingLabelTag = 7;
CGFloat LeftLabelWidth = 130.0;
static NSString *CellIdentifier = @"Cell";
ClientState *clientState = [ClientState sharedInstance];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.row == 0)
{
lblFacingValue = [[[UILabel alloc] initWithFrame:CGRectMake(246, -11, LeftLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblFacingValue.font = [UIFont systemFontOfSize:21];
lblFacingValue.textAlignment = UITextAlignmentLeft;
lblFacingValue.backgroundColor = [UIColor clearColor];
lblFacingValue.tag = kFacingLabelTag;
lblFacingValue.textColor = [UIColor yellowColor];
[cell.contentView addSubview:lblFacingValue];
}
} else {
lblFacingValue = [cell.contentView viewWithTag:kFacingLabelTag];
}
lblFacingValue.text = [NSString stringWithFormat:@"%@", clientState.Direction];
}
Upvotes: 1