Store boolean value in UITableviewcell - iphone

How can I store a boolean value in each row of a UITableview. I need to retrieve the boolean value stored in the cell, when that particular cell is selected.

There are so many ways:
1. You can use UITableViewCell.tag property
2. You can create your own cell class inherited from UITableViewCell and add there normal property for you BOOL value
3. You can use array associated with your tableview and when you get cell selected, just use indexPath to find associated value in your array
etc.

I recommend to use tag property in UITableViewCell.
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// returns nil if cell is not visible or index path is out of range
{
static NSString *identifier = #"MyIndetifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//make sure tu put here, or before return cell.
cell.tag = 0; //0 =NO, 1=YES;
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
BOOL boolean = cell.tag; // return 0 or 1. based on what boolean you set on this particular row.
}

You probably have some other storage, where you keep things like the table cell title or subtitle. Store the boolean there. Use this to convert the bool to something you can put in an array or dictionary.
[NSNumber numberWithBool:YES]
For example, if you're using an NSArray of strings to store the titles, instead use an array of dictionaries. Each dictionary would have a "title" and (for example) "isActive" boolean (stored as an NSNumber).

NSMutableArray *tableData;
every table cell associate with a NSMutableDictionary store in tableData,
you can set a NSNumber(store bool) to the Dictionary.

You need to implement method of UITableView
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// returns nil if cell is not visible or index path is out of range
{
//create cell here
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//by using indexpath.row you can access the cell on which user clicked.
}

Related

UITableView Reuse Causing Broken Cells

When scrolling my UITableView (tends to be when I scroll it fast) the data for my cells gets mixed up, so labels might be repeated etc.
I understand that reusing the cells probably causes this, but what if the user scrolls down the list really quickly and all the cells get mixed up, how am I supposed to avoid this?
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"VideoListCell";
VideoListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[VideoListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (isPlaylistView)
{
//Fill cell with data
}
else if (isPlaylistDetailView || isSearchResultsView)
{
//Fill cell with data
}
else
{
//Playlist button and uploads
if (indexPath.section == 0)
{
//Fill cell with data
}
else
{
//Fill cell with data
}
}
return cell;
}
You generally use this kind of code:
cell = dequeReusableCell;
if (cell == nil) {
create cell;
initialize cell;
}
fill cell with actual data from current row
return cell;
If you will move code "fill cell with actual data from current row" into "if" — you will get the kind of behavior you get right now.
So the answer will be "fill cell with data after you initialize it, outside of "if (cell == nil)" block.
UITableView will ever only dequeue a cell for reuse if the position that the cell was in is currently off-screen. So you don't have to worry about "mix-ups".
static NSString *cellIdentifier=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
I think it will be helpful to you.
set dequeueReusableCellWithIdentifier to nil for example..
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
UPDATE:
See this Example... i load many data in the cell with also my custom Gridview...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = #"gridCell";
UITableViewCell *gridCell = [tableView dequeueReusableCellWithIdentifier:nil];
if(gridCell == nil)
{
gridCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
return gridCell;
}
hope this help you....
In your custom cell class override prepareForReuse method. In this method set text of your labels to nil and set imageview's image to nil also. This function is called everytime when a cell is reused so your problem will be solved by this. May be like this
- (void)prepareForReuse{
[super prepareForReuse];
self.titleLabel.text = nil;
self.unitImageView.image = nil;
}

set text for cells in uitable different

I have a table with a few rows, and the place holder text for all is the same. How can I programmatically change the placeholder text of only the first one?
Thanks!
You need to run an if statement to check the index of the cell. 0 is the first index, so you would need to check to see if the row is 0.
- (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];
}
if (indexPath.row == 0)
cell.textLabel.text = #"First Cell";
else
cell.textLabel.text = #"Not First Cell";
}
In order to access your cell AFTER the cell was created/reused, try the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath method of your uitableview object! See the following Apple doc for a detailed elaboration on this method.
Note:
Return Value
An object representing a cell of the table or nil if the cell is not
visible or indexPath is out of range.

No text in my UITableViewCell

just created an UITableView,
trying to display some custom data from an array, but what ever I do, i get no text displayed.
NSLog tell me the right text and right amout but no text in table cell.
here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Things to check:
Is cellForRowAtIndexPath getting called? Put a breakpoint in
Make an array in ViewDidLoad (property, alloc'd) and addObjects #"One", #"Two", #"Three", nil and then cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
IfcellForRow is being called, this will show in the cells.
What doesConfigureCell do? Include the code please.
Also check your tableView delegate methods are being called (NumberofRowsInSection etc)
Always set cell properties (text, images, accessory views, etc) in the
tableView:willDisplayCell:forRowAtIndexPath
delegate method.
UITableView sometimes send a prepareForReuse to the cell after it is returned from
tableView:cellForRowAtIndexPath
which causes the cell to reset it's labels and images.

Prevent reloading of MKMapView inside UITableViewCell

I've made a subclass of UITableViewCell and added a MapView. Everytime I scroll the map of the screen and scroll back to that cell it loads again. (As you all know it's the default behaviour for cell reusing)
Is there anyway to prevent that? Or do you know any other tricks for that case? thanks a lot!
One possible solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSInteger row = [indexPath row];
UITableViewCell *cell = nil;
if( row != kMapCellRow ) { // defined value to whatever value it may be
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
else {
// use an ivar to track the map cell, should be set to nil in class init
cell = _mapCell;
}
if (cell == nil) {
if( row != kMapCellRow ) { // defined value to whatever value it may be
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else {
// create your map cell here
cell = [[[MyTableViewMapCell alloc] init...
}
}
// configure it here
You could replace the defined kMapCellRow with an instance variable if it makes sense for your app.

iphone - when exactly does cellForRowAtIndexPath for table views get called and by what in

I just have a basic (noob) question about cellForRowAtIndexPath get called?
I'm working through example code and I don't see it explicitly called anywhere?
Does any component of type UITableViewComponent automatically call this function when it is created?
Thanks
When a UITableView is displaying this method gets called per row. In it you will customize each cell with particular data for display.
Here's the class reference.
Here's a sample:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FriendCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
Item *i = [itemArray objectAtIndex:row];
cell.textLabel.text = [i name];
cell.detailTextLabel.text = [i brewery];
[i release];
return cell;
}
This is called for each cell when the corresponding UITableView gets drawn. An index path is passed in, and based on the section and cell numbers, your code should generate a UITableViewCell to be displayed in the UI.
You can look here for a quick tutorial into how UITableViews work.