iPhone Custom cell with UISegmentedControl - iphone

I have created a custom cell with a UISegmentedControl on it and loaded the cell like,
static NSString *CellIdentifier = #"Cell";
SegmentedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *cells =[[NSBundle mainBundle] loadNibNamed:#"SegmentedCell" owner:nil options:nil];
for (UIView *view in cells)
{
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (SegmentedCell *)view;
[cell.SegmentedControl addTarget:self
action:#selector(segmentedControlChanged:)
forControlEvents:UIControlEventValueChanged];
}
}
}
cell.textLabel.text = #"Sample";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
The custom cell successfully loaded and I get the action of the SegmentedControl.
But when I scroll the table view, state of the SegmentedControl is changed.

When you scroll a tableview that uses dequeueReusableCellWithIdentifier, you aren't actually saving all the different cells. To fix your problem you need to implement a few things.
Set your segmented control up to store its value to a variable every time it is pressed.
Put this value into an array
In the cellForRowAtIndexPath method, get the variable out of the array and set the segmented control's value to this value of the variable.

Related

TableView adding a cell

I'm doing a project in which I have a TableView in which I have different Rows/Cell and in each cell there is a add button. What I need to do is on clicking the add button the new row should add below the row in which I clicked the button, means I have to add a cell to the next of the selected button on the row.and also a user can enter a text on the new row. Please tell me how to do this . As I'm new in iPhone development. I will be very thankful ....
Here is a click button function in which I want to perform an action to add new cell.
- (IBAction)AddButtonAction:(id)sender
{
[_choices insertObject:#"avav" atIndex:[_choices count]];
[self.tableView reloadData];
NSLog(#"here");
}
Use this delegate method to add a new row below the selected row. And use custom cell to have a text field on it...
rowCount ++;//Here rowcount refers the no. of rows in the table.
selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it.
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell.
In cellforRowAtIndexPath use like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section))
{
static NSString *cellIdentifier=#"cell";
NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[NewCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (NewCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
// Configure the cell...
return cell;
}
}
OutPut will be like this
need to customize as per your requirement.
I consider you have textfeild in all cells. In AddButtonAction function save index of row. While creating cells make selected cell textfeild firstresponder. Once user enter data save it in array and again reload. Just keep track of selectedIndex properly.
Ifeel this will help.
If you add button to cell by addSubview method, simply you can get indexPath with the following code:
- (IBAction)AddButtonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[button superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Adding methods comes here...
}
If you are using some addSubview levels, you should use same superview methods inverse to access the UITableViewCell level. If using custom cells, replace your custom cell classname with UITableViewCell in the above code.
For adding cells, take a look at WWDC 2011 Videos, "UITableView Changes, Tips & Tricks" video. It shows some nice and useful methods to insert, delete and reload new rows/sections between other rows/sections like insertRowsAtIndexPaths:withRowAnimation: method. Or see Apple Documents.

UITableViewCell is hidden.

I have a UITableView in my controller. The cells for the UITableView have xib.
For some reason, when the table is loaded, the views of the cells are hidden.
I can select the cell, I see that the cell is not nil, and the views are not nil as well, still the cell is hidden.
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"CategoryCell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
for (id obj in topObjects){
if ([obj isKindOfClass:[CategoryCell class]]){
cell = (CategoryCell*)obj;
}
}
}
id object;
if (indexPath.row < items.count)
object = [items objectAtIndex:indexPath.row];
if ([object isKindOfClass:[MenuCategory class]]) {
// Configure the cell
MenuCategory *cellInfo = (MenuCategory *)object;
[cell setCategory:cellInfo];
}
else if([object isKindOfClass:[MenuSubCategory class]]){
// Configure the cell
MenuSubCategory *cellInfo = [self.items objectAtIndex:indexPath.row];
[cell setSubCategory:cellInfo];
}
return cell;
}
First:
NSArray* topObjects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
for (id obj in topObjects){
if ([obj isKindOfClass:[CategoryCell class]]){
cell = (CategoryCell*)obj;
}
}
Replace this with
[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
and declare an IBOutlet in your class that becomes the owner of the xib. This outlet can point to your cell. You then have to connect the Files Owner (has to be of the class the above code is in) within the Cell XIB to your cell. You can then reference your cell (after having declared a property for example) by
self.myCellOutlet;
and don't have to enumerate on all the objects within the xib.
Are you sure that this
[cell setSubCategory:cellInfo];
works?
If you really think your cell is just hidden, did you try to send it a
[cell setHidden:NO];
just in order to see if that's the case and if the error isn't different from the visibility state?
O so sorry wasting your time.
The solution is nothing to do with code.
The table was narrower then the cells. So the text (that was written on the left side of the cell) was outside of the table bounds. And that is why it was missing from the screen.

Reusing of Custom Cells

I taken a custom cell and i am not reusing the identifier over there. I am using the identifier in the table view where i am using that custom cells. But when i run my application and entering the data in textfield my data getting erased when i scroll the view.and My cell are not reusing that data.Can any one help me out in this thing. Thanks!
Custom cell code
-(UITableViewCell *)returnCellForEach:(UITableView *)tableView
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
myLabel = (UILabel *)[customCell.contentView viewWithTag:10];
return customCell;
}
Table view 'cellForRowAtIndexPath' code
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Hello"] autorelease];
}
whats mistake i this thing. Thanks!
Cells should not be used to store your data. If the user enters something in the edit field, you need to copy the content to e.g. a member variable in your view controller. If the cell is scrolled out of view, it is either destroyed, or it MIGHT be kept for reuse. Either way, you can not rely on that edit field to exist anymore.
I dont get your question actually, But I have done like following :
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
}
myLabel = (UILabel *)[customCell.contentView viewWithTag:10];
//myLabel = cell.labelWithTag10;
myLable.text = #"Text";
And It works fine for me;

How can i create a custom UITableViewCell?

I want to know how can i create a custom UITableViewCell with three parts in the UITableViewCell like that:
Create a blank XIB with a UITableViewCell and design the cell dropping elements on it. Set arbitrary tags (1,2,3...) on each element so you can retrieve them later.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"xxx";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NameOfTheBlankXIB" owner:self options:nil];
cell = [nibObjects objectAtIndex:0];
}
// get the elements inside the cell by tag and set a value on them
UILabel*label = (UILabel*)[cell viewWithTag: 1];
label.text = #"some text";
return cell;
}
Another way is to go to the NIB and set this view controller as the file owner and hook the the cell to a ivar of the view controller, like IBOutlet UITableViewCell *tableViewCell.
Now loading this with this instance as the owner automatically hooks the cell to the outlet tableViewCell.
[[NSBundle mainBundle] loadNibNamed:#"NameOfTheBlankXIB" owner:self options:nil];
// at this point the cell is hooked to the ivar so you get a reference to it:
cell = self.tableViewCell;

how to make a UIView added to UITableViewCell content view fit within its bounds?

how to make a UIView added to UITableViewCell content view fit within its bounds?
That is, I have created a single NIB file (with 3 labels on it), and want to use this for the display of each cell in a UITableView. I'm adding in the cellForRowAtIndexPath method the custom NIB based view to the cell's content view, however all I see as an end result is one (1) custom NIB based view (not multiple within a tableview as I expected).
How do I arrange so that each custom view fits neatly within each UITableViewCell? Also note the labels within the custom NIB view have word wrap.
Do I have to create a frame for the custom NIB view, but then in that case I'm not exactly sure how to set the co-ordinates. Would it be relative to the UITableViewCell? and if the custom view height can change due to it's UILabel word wrap, then I assume I separately have to manually calculate this height in the UITableView? (perhaps I should go to dynamic/custom view creation and drop the concept of using InterfaceBuilder/NIB)
- (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];
}
UIView *detailedView = [[[NSBundle mainBundle] loadNibNamed:#"DetailedAppointView" owner:self options:nil] objectAtIndex:0];
[cell.contentView addSubview:detailedView]; // DOESN'T SEE TO WORK
return cell;
}
This is how I do it if I need to implement a custom UITableViewCell.
I create a subclass of UITableViewCell for my custom cell ex. "MyCustomCell". Then I create a NIB file for it, and within this NIB file I insert UITableViewCell and change it's type to "MyCustomCell" and I give it an identifier with the same name as the class name. Then I insert all my subviews onto my cell. All in IB.
After I got my cell pimped out to my likning :-) I use it in the following way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
static NSString *CellNib = #"MyCustomCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MyCustomCell *)[nib objectAtIndex:0];
}
//Manipulate your custom cell here
return cell;
}