How to set a new value to the textfield inside the UITableViewCell? - iphone

Does this code triggers when I call [Self.table reloadData]
if (cell == nil){...}
Im wondering because I have added a UITextField in the cell:
//cellForRowAtIndexPath
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
recepientTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+40, 10, 469, 20)];
[cell addSubview:recepientTextField];
}
recepientTextField.text = recipient;
return cell;
Then I download some stuff from internet and I then want to show the info in recepientTextField.text in the TableView. So I call set a new value to recipient variable and then [self.tableView realoadData] But it crashes. I think it has to do with the textfields allocation. How can I fix this? How can I change the text afterwards?

When reloaded the cell != nil case comes and there the recipientTextField has null reference and hence the crash
Try this
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
recepientTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+40, 10, 469, 20)];
[recepientTextField setTag:333];
[cell addSubview:recepientTextField];
}
UITextField *TF=(UITextField *)[cell viewWithTag:333];
TF.text = recipient;
return cell;

Related

Error [IRForTarget] in UITableView

I am having more than a table view in a view controller, it is giving error. I have no idea what is where going wrong can anyone guide. Data is not showing in table view but array has data when printed in console, i tried without array also providng static data to cell.textLabel.text, but not showing in tableview, numbers of rows checked no issue there. control does not go to cell.textLabel.text, i am not getting what is wrong. Please guide for the above. Below is the code:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
if(tableView == self.mLocationTable)
{
cell = [self.mLocationTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// if([self.mLocShowArr count]!= 0)
// {
// NSString *str = [self.mLocShowArr objectAtIndex:indexPath.row];
cell.textLabel.text = #"locations";
// NSLog(#"show loc:%#", [self.mLocShowArr objectAtIndex:0]);
// }
}
if(tableView == self.mNotifyTable)
{
cell = [self.mNotifyTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImage *yourImage = [UIImage imageNamed:#"emma-watson-02.jpg"];
UIImageView *imageViewToPutInCell = [[UIImageView alloc] initWithImage:yourImage];
imageViewToPutInCell.frame = CGRectMake(5, 8, 55, 55);
UILabel *cellTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 10, 200, 40)];
cellTextLabel.text = #"notification view";
[cell addSubview:imageViewToPutInCell];
[cell addSubview:cellTextLabel];
}
}
This is the complete error : Error [IRForTarget]: Call to a symbol-only function 'objc_msgSend' that is not present in the target.
Things to check.
Data source methods all required implemented
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
log the return value and make sure it is not 0
Give the different cell identifier for different tables
Make sure table data source and delegates are connected in the nib
Error Explanation here
Try this code !!! In your code you missed "return cell;"
-(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.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(tableView == self.mNotifyTable) {
cell.textLabel.text = #"locations";
UIImage *yourImage = [UIImage imageNamed:#"emma-watson-02.jpg"];
UIImageView *imageViewToPutInCell = [[UIImageView alloc] initWithImage:yourImage];
imageViewToPutInCell.frame = CGRectMake(5, 8, 55, 55);
UILabel *cellTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 10, 200, 40)];
cellTextLabel.text = #"notification view";
[cell addSubview:imageViewToPutInCell];
[cell addSubview:cellTextLabel];
}
return cell;
}
Hope this solves your problem !!!
Accept the answer if it helps you !!!

Tableview records keep repeating and overlapping

This is my cellForRowAtIndexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12,11,320,40)];
label.text = [animalArray objectAtIndex:indexPath.row];
[cell addSubview:label];
In the animalArray i have the following items,
Lion, Dog, Cat, Zebra, Donkey, Monkey...
The problem is;
1.) When i add the label inside the if (cell == nil) { condition i see repeated records. like Lion, Dog, Cat, Zebra and then again Lion as i scroll down.
2.) When i add it outside the if (cell == nil) { the records overlap as i scroll.
I understand that this is because the cell is reusing its self, and i am creating a Label each time (so it gets overlapped). I don't know how to fix this issue programatically. Can someone please help me.
EDIT:
I have just displayed 1 label here. but i have more than one label to display. and also some UIComponants.
I writing this code without checking syntax:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12,11,320,40)];
label.tag = 556;
[cell.contentView addSubview:label];
}
UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:556];
cellLabel.text = [animalArray objectAtIndex:indexPath.row];
you dont have to add label to your table cell. it already has cell.textLabel.text=#"some string".
replace this part:
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12,11,320,40)];
label.text = [animalArray objectAtIndex:indexPath.row];
[cell addSubview:label];
with this:
cell.textLabel.text=[animalArray objectAtIndex:indexPath.row];
The best approach would be subclassing UITableViewCell and adding all labels. That way you need to set labels' text properties outside of the
if (cell == nil){...}
block.
use this by removing if(cell == nil)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12,11,320,40)];
label.text = [animalArray objectAtIndex:indexPath.row];
[cell addSubview:label];
[label release];

UITableViewCell is being reused but I can't work out why?

I've got a tableview loaded from an array and for some reason it's re-using the cell but loading the same information lower down the table, a second time.
http://screencast.com/t/Ig8bcqSpLzp
The video above should give you an idea of what I mean.
This is my code to load the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
accessicon *current =[arryAccess objectAtIndex:indexPath.row] ;
cell.textLabel.text = current.text;
cell.imageView.image = [UIImage imageNamed: current.iconPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
switchView.myValue = current.iconValue;
if(totalIconValue & current.iconValue) {
[switchView setOn: YES animated:NO];
} else {
[switchView setOn: NO animated:NO];
}
[switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
return cell;
}
Any help will be appreciated
EDIT: I've updated my code above, however on the actual device it's still selecting items it shouldn't be - no longer reusing the names but if i scroll up and down it selects them on it's own?
see the video here: http://screencast.com/t/a9N1qbws
Tom
This code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil) {
...
is fundamentally wrong. It should read:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
...
And then, anything that is row-specific should be outside that if statement.
The pattern for this, generally, should be:
dequeue a reusable cell
if you didn't get one:
allocate a new cell
perform generic cell setup, i.e. setup that applies to all cells
setup the cell for the specific row, and return it.
Hope this helps.
You aren't reusing you cells. And you are filling the cells only when the init fails. Try this code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
accessicon *current =[arryAccess objectAtIndex:indexPath.row] ;
cell.textLabel.text = current.text;
cell.imageView.image = [UIImage imageNamed: current.iconPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
switchView.myValue = current.iconValue;
if(totalIconValue & current.iconValue) {
[switchView setOn: YES animated:YES];
} else {
[switchView setOn: NO animated:YES];
}
[switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
Or develop under ios5 and get rid of the alloc and init.
You are constantly creating new rows instead of reusing the old ones.
Move the line
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
inside the if block, end move everything else you have in the if block outside of it, except the lines
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
You need to create the switch only once for row, when it is initialized (that means, in the if statement). I suggest you to create it and assign it a tag, so you can access it later by its tag. Your code now is creating a new switch everytime the cell is used, this could be the reason why the switches are behaving this way.

iPhone/iPad SimpleTableViewCells: What replaces the deprecated setImage setText on table cells?

iPhone/iPad SimpleTableViewCells: What replaces the deprecated setImage setText on table cells?
The below code gives warnings that setImage and setText are deprecated. So what replaced them? What is the new better way to get this simple behavior?
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.image=[UIImage imageNamed:#"Wazoo.png"];;
cell.text = #"Wazoo";
So what is the simplest way to have the same affect as image/text of a cell without doing a lot of work or getting warnings?
With new release of iOS SDK, setText and setImage properties are deprecated and they are replaced by textLabel.text for setText and imageView.image for setImage...
With this new properties, your code would be:
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:#"Wazoo.png"];;
cell.textLabel.text = #"Wazoo";
This properties are usually used when the cell uses preimpostated styles like UITableViewCellStyleDefault, UITableViewCellStyleSubtitle, UITableViewCellStyleValue1 and UITableViewCellStyleValue2 or CGRect...
If you will display also a subtitle, the code to be used is:
cell.detailTextLabel.text = #"Your Subtitle Here!";
You call textLabel.text and imageView.image on the cell.
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
cell.imageView.image=[UIImage imageNamed:#"Wazoo.png"];;
cell.textLabel.text = #"Wazoo";

UITableView add control issue in iphone

I am adding an activity indicator in each row of table. The issue is every time I scroll it get added again in cell overwriting the previous one. Please let me know which is best way to add control in tableview cell.
UIActivityIndicatorView *a;
// Customize the appearance of table view cells.
-(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];
}
a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[a startAnimating];
[cell.contentView addSubview:a];
// Configure the cell.
return cell;
}
Add indicator to a cell only when you create it, then you can access it using its tag property:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
a.tag = 10;
[cell.contentView addSubview:a];
}
UIActivityIndicatorView* aView = [cell.contentView viewWithTag:10];
[aView startAnimating];