Reusable cell in UITableView - iphone

I'm using a UITableView and a custom cell with a checkbox.
I have more than 1 section. When a check a checkbox in the first section, for example the cell with row = 0 and section = 0, I save the data and it works. But the cell in the row = 0 and section = 1 is also checked! How can I make the difference between those sections ?
Thank you so much!

Following sample code will help you for your situation.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (CustomCell *) [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.checkBox = [self fillCheckBoxStatus:indexPath];//This method will say about check box whether going to SET or NOTSET.
//...
return cell;
}

use dequeueReusableCellWithIdentifier to nil like bellow...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
////you another code..
}
return cell;
}

Related

My custom cell does not display

I have created a custom cell that contain a Label but when I added that in my Table View than it does not display, my view controller is not TableViewController I have seted Table View using IB and seted its delegate and datasource correctly.
I am doing this by:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
return theDataObject.myAudio.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];
NSArray *parts = [destinationPath componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
AudioInfoCell *cell = (AudioInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.audioName.text = filename;
return cell;
}
First check how many row set in tableView:numberOfRowsInSection method
After set the custom cell like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AudioInfoCell *cell = (AudioInfoCell *) [tableView dequeueReusableCellWithIdentifier:nil];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AudioInfoCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AudioInfoCell *) currentObject;
break;
}
}
}
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];
NSArray *parts = [destinationPath componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
cell.audioName.text = filename;
return cell;
}
Two things to check:
Make sure the reuse identifier matches what is in Interface Builder
Make sure your numberOfRowsInSection and numberOfSectionsInTableView
return the right numbers
Also - I don't use:
if (cell == nil) {
cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
for instantiating the cell. That should be taken care of in the AudioCell TableViewCell subclass you have implemented. An example of how I implement this kind of custom cell is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"FlightPickerCell";
FlightPickerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.tripIDLabel.text = #"Some TripID";
cell.hospitalLabel.text = #"Some Hospital";
cell.cityLabel.text = #"Some City";
return cell;
}

create a richtext uitableviewcell with uiwebview

Well basically I am trying to pass a string to a uiwebview that just so happens to be in html format, however I am passing it to the uiwebview using loadHTMLString:myHTML.
the problem that I am having is that I cannot see any text at all. I have set up a custom view which I have then deleted the view, added a uitableviewcell then inside the cell I have added a uiwebview. I have then set the related class to the class in which I would like to display the tableviewcell, then linked my getter/setter to the tableviewcell and the uiwebview inside the tableviewcell..
Then this is the code that follows with me trying to set the richtext of the uiwebview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {
if (indexPath.row == 0) {
// Set cell height
[self tableView:tableView heightForRowAtIndexPath:indexPath];
// Configure the cell using custom cell
[[NSBundle mainBundle] loadNibNamed:#"SearchCellHtml" owner:self options:nil];
cell = searchCellHtml;
// pass in some data into myHTML
myUIWebView = [[UIWebView alloc] init];
NSString *myHTML = #"<html><body><h1>Hello, world!</h1></body></html>";
[myUIWebView loadHTMLString:myHTML baseURL:nil];
//Disclosure Indicator
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
any help with this would be greatly appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {
if (indexPath.row == 0) {
[self tableView:tableView heightForRowAtIndexPath:indexPath];
// load the cell ?
cell = [[NSBundle mainBundle] loadNibNamed:#"SearchCellHtml" owner:self options:nil];
myUIWebView = [[[UIWebView alloc] init] autorelease]; // autorelease
NSString *myHTML = #"<html><body><h1>Hello, world!</h1></body></html>";
[myUIWebView loadHTMLString:myHTML baseURL:nil];
//add webView to your cell
myUIWebView.frame = cell.bounds;
[cell addSubview:myUIWebView];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

Add image and text to tableview

I try to add some text taken from a textfield and an image taken from photo album to UITableViewCell.
In one UIViewController I have a UITextField and a UIImageView. This is the code I use to
populate an array of products that I would present in cells of a UITableView.
Article *article1 = [[Article alloc] initWithTitle: #"First Text" Subtitle: #"Data&Time" Thumbnail: [UIImage imageNamed: #"image.png"]];
articles = [[NSMutableArray alloc] init];
[articles addObject:article1];
Do you want the cells to display the properties of your Article objects? Let's assume you only have one section and your UITableViewController already has a reference to an array of Articles called "articles".
- (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];
}
Article *article = [articles objectAtIndex:indexPath.row];
cell.textLabel.text = article.title;
cell.detailTextLAbel.text = article.subtitle;
cell.imageView.image = article.thumbnail;
return cell;
}
You have to inherit UICell class, there are too many examples. Just google smth like "custom UITableView" or "custom UITableViewCell".
Also, there is left image for cell in Cocoa.
ADD a custom cell to display image to the table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MoreDetailCell";
MoreDetailCell *cell = (MoreDetailCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MoreDetailCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MoreDetailCell *)currentObject;
break;
}
}
}
cell.compImage.image = [UIImage imageNamed:#"Placeholder.png"];
cell.yourtexfieldname.text = #"ABC";
}

Data is mismanaged when I scroll the UITableView

I am using following code,in cellForRowAtIndexPath:(NSIndexPath *)indexPath method
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(currentRow >=[finalArray count]) return cell;
// Configure the cell.
for(int ii=0;ii<[keys count];ii++){
NSString *no22=[dict objectForKey:[keys objectAtIndex:ii]];
no=[NSString stringWithFormat:#"%# %"[keys objectAtIndex:ii],]
}// end of the for loop
if(searching)
cell.text = [copyListOfItems objectAtIndex:indexPath.row];
else {
cell.text=[finalArray objectAtIndex:currentRow];
currentRow=currentRow+1;
}
return cell;
}
I am getting strange behavior when I scroll the table data is completely mismanaged,please any suggestion.
Is because you are reusing cells. You can reuse more type of cells.
And why you are doing if(currentRow >=[finalArray count]) return cell; this?

I got Error while using UITableView as 'setText:' is deprecated

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.