Reputation: 199
i am new in ios world, i have some data to be loaded in table view controller and thats load perfectly.. now if a new insert were done the new data is added to a NSMutableArray starting from index 0 (new data is at the top and old data are after them) now after updating the NSMutableArray with the new data i call [tableview reloadData].
my aim is to show the new data at the top of table view controller (index 0) but nothing of the new data is loaded ! why ?
this is the code i use to load table cell, where data is the NSMutableArray containing my objects :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
// add description
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
text.numberOfLines=3;
text.text= [[data objectAtIndex:indexPath.row] desc];
text.backgroundColor=[UIColor clearColor];
text.textAlignment= UITextAlignmentRight;
text.font = [UIFont fontWithName:@"Helvetica" size:13];
[cell addSubview:text];
}
//use your cell here
cell.textLabel.textAlignment=UITextAlignmentRight;
cell.textLabel.numberOfLines =3;
// cell font
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
Upvotes: 0
Views: 156
Reputation: 12211
Try the following things:
1.Change your code to below , it should work:
move your text UILabel as a iVar in the MyCell and add it there to the cellView. Just configure the information alone based on different index outside the initialization block.
(It is a good practice to move static style methods to the initialization phase itself. )
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
cell.text.numberOfLines=3;
text.backgroundColor=[UIColor clearColor];
cell.text.textAlignment= UITextAlignmentRight;
cell.text.font = [UIFont fontWithName:@"Helvetica" size:13];
// [cell addSubview:text];
cell.textLabel.textAlignment=UITextAlignmentRight;
cell.textLabel.numberOfLines =3;
// cell font
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
cell.textLabel.textColor=[UIColor blackColor];
}
cell.text.text= [[data objectAtIndex:indexPath.row] desc];
//use your cell here
Upvotes: 0
Reputation: 2830
If you look at the code your putting in your only assigning the value to the UILabel the first time the cell is created, on any dequeue methods where cell != nil then you just change the font and color for the label already in the cell since your using StyleValue1
// add description
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
text.numberOfLines=3;
text.text= [[data objectAtIndex:indexPath.row] desc];
text.backgroundColor=[UIColor clearColor];
text.textAlignment= UITextAlignmentRight;
text.font = [UIFont fontWithName:@"Helvetica" size:13];
[cell addSubview:text];
You do not need this if you just use the label that is associated already with the cell, otherwise if you do need this then you should give the label tag so that you can retrieve it again from the cell when you go to display.
Also your adding the subview to the cell instead of the content view of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{ UILabel *text; NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
// add description
text = [[UILabel alloc] initWithFrame:CGRectMake(15,25, 260, 50)];
text.numberOfLines=3;
text.backgroundColor=[UIColor clearColor];
text.tag = 1000;
text.textAlignment= UITextAlignmentRight;
text.font = [UIFont fontWithName:@"Helvetica" size:13];
[cell addSubview:text];
} else {
text = (UILabel*)[cell viewForTag:1000];
}
// Here you update the value of text
text.text= [[data objectAtIndex:indexPath.row] desc];
//use your cell here
cell.textLabel.textAlignment=UITextAlignmentRight;
cell.textLabel.numberOfLines =3;
// cell font
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
Upvotes: 1
Reputation: 15217
I think you have a misunderstanding: A table view cell is normally an object of a certain structure (say, a title and a subtitle) that is identified by an identifier. So, in the simplest case, all cells of a table view have the same structure and thus the same identifier. The reason is that a table view cell can be re-used: If a cell is scrolled out of the screen, it is put back into a pool, and when a new cell is scrolled into the screen, a cell of the pool is used to display new data. In this case it make no sense at all to create a new identifier for each row of the table.
You problem might be that you ask the table view for a cell with an unknown identifier, which you create dynamically using NSString *identifier = [NSString stringWithFormat:@"cell%d", indexPath.row];
I suggest using a single table view cell type, identified by a single identifier, and setting its property cell.textLabel.text
accordingly.
Upvotes: 0