Ketan Shinde
Ketan Shinde

Reputation: 1847

reloading texts in labels of uitableview

now i am getting text in labels of table view when i update item array that is globally declared at 0 index and return back then it wont update the row in table view row at index 0 but when i update other rows then it updates. And also when i scroll table view towards upward then it shows value at index 0 on all index. here is my code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath {

 static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) 
  {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];    
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:@"Arial" size:16];
[cell addSubview:nameLabel];

deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame= CGRectMake(3, 3, 30, 30);
[deleteButton setImage: [UIImage imageNamed:@"cross_btn.png"] forState:UIControlStateNormal];
[cell addSubview:deleteButton];

priceMark = [[UILabel alloc]initWithFrame:CGRectMake(200, 8, 60, 25)]; //150, 8, 65, 25
priceMark.backgroundColor = [UIColor clearColor];
priceMark.font = [UIFont fontWithName:@"Arial" size:14];
[cell addSubview:priceMark]; 

qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 8, 65, 25)]; //225, 8, 60, 25
qtyLabel.backgroundColor = [UIColor clearColor];
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14];
[cell addSubview:qtyLabel];
 }
 nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"];
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray 
objectAtIndex:indexPath.row] objectForKey:@"amount"]];
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"];

return cell;
 }

Upvotes: 1

Views: 148

Answers (1)

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

change your code to:

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) 
  {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];    
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:@"Arial" size:16];
//added
nameLabel.tag = 101;
[cell addSubview:nameLabel];

deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame= CGRectMake(3, 3, 30, 30);
[deleteButton setImage: [UIImage imageNamed:@"cross_btn.png"] forState:UIControlStateNormal];
//added
deleteButton.tag = 102;
[cell addSubview:deleteButton];

priceMark = [[UILabel alloc]initWithFrame:CGRectMake(200, 8, 60, 25)]; //150, 8, 65, 25
priceMark.backgroundColor = [UIColor clearColor];
priceMark.font = [UIFont fontWithName:@"Arial" size:14];
//added
priceMark.tag = 103;

[cell addSubview:priceMark]; 

qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 8, 65, 25)]; //225, 8, 60, 25
qtyLabel.backgroundColor = [UIColor clearColor];
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14];
//added
qtyLabel.tag = 104;

[cell addSubview:qtyLabel];
 }

//Added
nameLabel = (UILabel*)[cell viewWithTag:101];
deleteButton = (UIButton*)[cell viewWithTag:102];
priceMark = (UILabel*)[cell viewWithTag:103];
qtyLabel = (UILabel*)[cell viewWithTag:104];
//

 nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"];
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray 
objectAtIndex:indexPath.row] objectForKey:@"amount"]];
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"];

return cell;
 }

Upvotes: 2

Related Questions