is it possible to add 2 cell.textlabel into the table view. As i want trying to display 3 different lines.
Here the code i did :
- (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];
}
cell.textLabel.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
cell.textLabel.numberOfLines = 2;
cell.textLabel.text = [NSString stringWithFormat:#"%# %#","Walk to and board at ",[boardDescArray objectAtIndex:indexPath.row]];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#","Alight Destination = ",[alDescArray objectAtIndex:indexPath.row]];
return cell;
}
but when i put two cell.textLabel.text it got an error bad access. what should i do?
Pls help
You only call cell.textLabel.text once, but put a \n in your string where you want a line break.
You also have an error in your stringWithFormat -- either put an # in front of "Walk to and board at" or just remove that first %#, and have :
stringWithFormat:#"Walk to and board at %#",[boardDescArray objectAtIndex:indexPath.row]];
So, to make it 2 lines you want this:
cell.textLabel.text = [NSString stringWithFormat:#"Walk to and board at %#\nAlight Destination = %#",[boardDescArray objectAtIndex:indexPath.row],[alDescArray objectAtIndex:indexPath.row]];
Related
I want to show a picture if each row starts with a certain character, let's say "&" in the below example. The code below seems to only work if the first row doesn't start with '&'. If the first row has '&' all of the rows will have the picture. What's wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
if ([[[array objectAtIndex:(indexPath.row)] substringToIndex:1] isEqualToString:#"&"])
{
cell.textLabel.text = [array objectAtIndex:(indexPath.row)];
cell.detailTextLabel.text = [array objectAtIndex:(indexPath.row)+1];
cell.imageView.image = [UIImage imageNamed:#"picture.png"];
return cell;
}
else
{
cell.textLabel.text = [array objectAtIndex:(indexPath.row)];
cell.detailTextLabel.text = [array objectAtIndex:(indexPath.row)+1];
return cell;
}
}
Since the first cell may be reused later on, you have to "reset" its state on every call of cellForRowAtIndexPath. Try adding cell.imageView.image = nil after cell.detailTextLabel.text in the else block.
I know there's similar questions to this, but the approved answers don't seem to be working for me. So, my scenario is that I have a UITableView and I want to add and remove items by scanning a bar code. All that works fine except I can't get the UITableView to display the updated information. The problem specifically comes from the tableView:cellForRowAtIndexPath: method on every other reload after the initial one. More specifically the cell is always not nil, so it skips over the new cell creation logic.
For other questions like mine, the answer is that the cell identifier is the problem. Well, I tried messing around with that and it didn't work. Here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:#"ModelCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"ModelCell"] autorelease];
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"category == %#", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:#"#distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"model == %#", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"RemoveCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RemoveCell"] autorelease];
cell.textLabel.text = #"Remove an Item";
cell.textLabel.textColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
return cell;
}
So, in this example code, I have two different cell identifiers for the separate sections. They are ModelCell and RemoveCell. Well, they don't work as a solution because nothing happens. If I change the cell identifier when I'm allocating a new cell it works because it's simply wiping everything since the identifiers don't match, but I'm going to assume that that is wrong and that there should be a better solution to this issue or I'm simply not doing something right.
I'd appreciate some help on this. I've spent a day on this chunk of code so far and I have not gotten anywhere and I'd like to get it fixed and move on to other part of my app...
Thanks in advance for any help!
UPDATE
Thanks to #fluchtpunkt the problem has been resolved. For anyone else who may run into this in the future, here's the corrected code. I decided to make the identifier even more unique by appending the section number to it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section < vehicle.inventoryCategoriesCount) {
NSString *identifier = [NSString stringWithFormat:#"ModelCell-%d", indexPath.section];
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"category == %#", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:#"#distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"model == %#", model]] count]];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"RemoveCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RemoveCell"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor redColor];
}
cell.textLabel.text = #"Remove an Item";
}
return cell;
}
UPDATE
The final code version which corrects my misunderstanding of how the identifier works. I figured I'd keep it simple, so I named the identifiers after the cell style type.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:#"Value1"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Value1"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"category == %#", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:#"#distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"model == %#", model]] count]];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"Default"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Default"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor redColor];
}
cell.textLabel.text = #"Remove an Item";
}
return cell;
}
put your cell configuration outside of if (cell == nil) { ... } The if condition is only true if no cell could be reused. And you definitely want to reuse your cells. So configure them when you have a valid cell.
Like this:
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:#"ModelCell"];
if (cell == nil) {
// only create a new cell if a dequeue was not successful.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"ModelCell"] autorelease];
}
// whatever happened before you have a valid cell here.
// configure cell:
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"category == %#", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:#"#distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"model == %#", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if you want to optimize your code you could move the options that are the same for every cell inside the (cell == nil) condition. For example setting the selectionStyle, or changing the textcolor
Alex - if I look at your code right at the top, you're doing this:
cell = [tableView dequeueReusableCellWithIdentifier:#"ModelCell"];
However, all the examples (and my code) require a static NSString for the CellIdentifier
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
This then works the intended way.
You definitely reuse your cells . if you put your configuration inside if(cell == nil) , it works when no cell is reused . so please put the following code snippet outside of if(cell == nil) condition------------
if(cell == nil)
{``
// enter code here
}
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"category == %#", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:#"#distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"model == %#", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITableView is crashing on scrolling . Here is the sample code..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Total Count: %d",count);
return [ObjectArray count] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:12.0];
cell.detailTextLabel.font = [UIFont fontWithName:#"Arial" size:10.0];
[cell.textLabel sizeToFit];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
// Configure the cell.
NSLog(#"Row Number %d", indexPath.row);
cell.textLabel.text = [ObjectArray name];
return cell;
}
In console I can see Total count = 22.
Row Number 0
Row NUmber 1
.
.
Row Number 21 (This row number will pull the last object from ObjectArray)
Now if I try to scroll it crashes...why? Can anyone help me with this ...
I don't really understand this line of code
cell.textLabel.text = [ObjectArray name];
If ObjectArray is a NSArray instance, then it would not respond to that selector. And anyway you probably want to return smth like
cell.textLabel.text = [ObjectArray objectAtIndex: [indexPath row]];
I got Error while using UITableView as 'setText:' is deprecated in line cell.text=cellvalue
Please anyone can how to fix this?
- (UITableViewCell *)tableView:(UITableView *)atable cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [atable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue; //Error 'setText:' is deprecated
return cell;
}
cell.textLabel.text = #"Value"
Because Apple added the Subtitle style to the table view cells, you need to use:
[cell.textLabel setText:#"String"];
instead.
on my tableview i have the last cell that is not initially visible as seen in the first image, when i scroll the list up, you can see in the second image that the price or my detailTextLabel is put on a new line not maintaining the right justification.
image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.jpg
image 2 http://img706.imageshack.us/img706/6007/iphoneerror2edited.jpg
Here is the code, i can't figure out why its doing this, any direction or help would be much appreciated
- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
NSUInteger indexRow = [indexPath row];
switch (indexRow) {
case 0:{
NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
NSString *description = [[currentData objectForKey:#"Description"] stringByTrimmingCharactersInSet:set];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cellShift = 1;
if (![description isEqualToString:#""]) {
cell.textLabel.text = #"";
cell.detailTextLabel.text = description;
cell.detailTextLabel.numberOfLines = 2;
}
else {
cell.textLabel.text = #"";
cell.detailTextLabel.text = #"";
cell.detailTextLabel.numberOfLines = 0;
}
break;
}
default:{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *item = [tableData objectAtIndex:(indexRow-cellShift)];
NSString *name = [item objectForKey:#"Name"];
if ([name length] > MaxVendorsLength ) {
name = [NSString stringWithFormat:#"%# ...",[name substringToIndex:MaxVendorsLength]];
}
cell.textLabel.text = name;
cell.textLabel.minimumFontSize = 12;
NSString *priceString;
float price = [[item objectForKey:#"Price"] floatValue];
//NSLog(#"| %# | : | %# |",[item objectForKey:#"Name"], [item objectForKey:#"Price"]);
if (price != 0) {
priceString = [[NSString alloc] initWithFormat:#"$%.2f",price];
}
else {
priceString = [[NSString alloc] initWithString:#"--"];
}
cell.detailTextLabel.text = priceString;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[priceString release];
break;
}
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.textLabel.minimumFontSize = 14;
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.minimumFontSize = 14;
return cell;
}
Let me know if i need to post anything else to get help with this ???
It's because when you scroll up the top cell is reused because all of your cells have the same cell identifier (the first line you have). You need to declare the two cell identifiers and use the appropriate one based on what row you're trying to get.
static NSString *FirstRowCellIdentifier = #"A";
static NSString *OtherRowCellIdentifier = #"B";
NSString *cellIdentifier = nil;
if ([indexPath row] == 0)
cellIdentifier = FirstRowCellIdentifier;
else
cellIdentifer = OtherRowCellIdentifier;
UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:cellIdentifier];
// .....
Then you can use the rest of your code as is. This just ensures that the resused cell is of the correct type.
You're using the same cell identifier for all rows but the 0th row has a different style. When scrolling up, a subtitle-styled cell might be getting re-used.
Try using a different cell identifier for the 0th row. Keep just the declaration of cell outside the switch and move the dequeueReusableCellWithIdentifier to each case.
(UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {