Reputation: 3622
I have a UITableView, created via the storyboard, with the cell style set to "Right Detail". This properly displays both the textLabel and the detailTextLabel if, and only if any of the following are true:
However, if I just have the single entry that I haven't done anything with, only the textLabel shows up.
My code is fairly basic. I have an array, which I populate with tab-delimited textLabel/detailTextLabel values. Then, in my tableView:cellForRowAtIndexPath: function, I have the following code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"default"];
NSDictionary *itemDict=[scanArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[itemDict valueForKey:@"quantity"];
cell.textLabel.text=[itemDict valueForKey:@"item"];
[cell setNeedsLayout];
[cell setNeedsDisplay];
return cell;
}
Whenever I make a change to the scanArray object, I call the reloadData function on my table view. This apparently works, because the textLabel does show up in the tableview, however as mentioned the detailTextLabel does not (initially).
Edit: Note that the content of the table is NOT static - the user will be adding and deleting items throughout normal use. It is simply that the problem only occurs immediately after the first item has been added to the list. Once the user adds a second item to the list (or performs any of the other actions I noted above), the problem goes away.
It has also been pointed out that storing my data as an array of dictionaries, rather than tab-delimited text, would be better. I agree, and will be making that change.
Edit2: The table is updated in response to a button click by the user. The user enters two values (item number and quantity), and taps the button. The following code is then run:
NSDictionary *itemDict=[NSDictionary dictionaryWithObjectsAndKeys:
self.ItemCode.text,@"item",
self.Quantity.text,@"quantity",
returnedData,@"id", nil];
[scanArray insertObject:itemDict atIndex:0]; //The most recent item is always the first item in the list.
[self.ItemCode setText:@""]; //clear out the entry fields
[self.Quantity setText:@""];
[self.HistoryTable reloadData]; //reload the table view.
I do other stuff with the data here as well (specifically transmitting it to a server), but that has nothing to do with the tableView.
Upvotes: 0
Views: 460
Reputation: 3622
One solution I have found that works is to replace the line:
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"default"];
with:
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"default"];
and remove the prototype cell from the storyboard. I have read, however, that this is not the best practice.
Upvotes: 1