insertion and deletion in the table of objective C - objective-c

I am wondering, I have an empty table and I want to display a text in that table, then I want to clear that text when the table has been inserted with something else. And the text will appear again when I completely remove everything in the table again.?

In your dataSource for your UITableView there is a method:
-tableView:objectValueForTableColumn:row:
In that method you can check your NSArray or whatever you have supplying data to your table to see if there are any entries. If there are none and the request is for row zero, column zero, just return your 'no data' string.
To clarify syntax, assume you have an NSArray named songs which you wish to print:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//boilerplate cell production
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//check if we have data to supply, if not set message to "No Songs Available"
//otherwise print the name of the song in each cell
if (indexPath.row == 0 && indexPath.section == 0 && [songs count] == 0)
cell.textLabel.text = #"No Songs Available";
else if (indexPath.row < [songs count])
cell.textLabel.text = [(Song *)[songs objectAtIndex:indexPath.row] songName];
return cell;
}

Related

Search Bar in UITableView doesn't display text in Cell label

Here's where the magic isn't happening:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"songCell";
songViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
long row = indexPath.row;
if (cell == nil) {
cell = [[songViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.songLabel.text = _searchResults[row];
} else {
cell.songLabel.text = _songListArray[row];
}
return cell;
}
I know the _searchResults array is populated with the correct search results and I've edited the numberOfRowsPerSection appropriately. The filter is working correctly, but it won't display the _searchResults[row] text while typing into the search bar. If I don't use the bar, the cells are populated correctly with _songListArray[row].
Something is going wrong in:
if (cell == nil) {
cell = [[songViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If I don't include the if expression, I get an error. How should I initialize the prototype cell while the search bar is in use? All I get is empty labels, but I know the search is working because if there are no filtered results in the array the table says "No Results" in the middle. Why isn't my songLabel.text updating?? It won't even display text when I set the label to #"HELLO??" instead of the array[row].
You need to have the searchResultsTableView dequeue a cell, not just your main table view. The cellForRowAtIndexPath method should look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView) {
RDTriangleCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
return cell;
}else{
UITableViewCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:#"SearchCell" forIndexPath:indexPath];
//RDCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:#"SearchCell" forIndexPath:indexPath];
cell.textLabel.text = self.filteredData[indexPath.row];
return cell;
}
}
In viewDidLoad, you should register the class if you use the code I show above for a standard UITableViewCell.
[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"SearchCell"];
If you use a custom cell for the search results table, then you should use something like the line I have commented out, and register the nib for that custom cell.

UITableView and datasource

I'm trying to add a data source inside a UITableView. I tried the following, but unfortunately it didn't work:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 8;
}
// 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];
}
// Set the data for this cell:
cell.textLabel.text = [_classCellview objectAtIndex:indexPath.row];
cell.detailTextLabel.text = #"One";
cell.detailTextLabel.text = #"Two";
// set the accessory view:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
cell.textLabel.text = [_classCellview objectAtIndex:indexPath.row];
You're telling the table that there are 8 sections, but you're not taking the section from the index path into account when you populate the cell. An index path has (in the case of a table view) two values: a section and a row. You need them both to know what cell you're dealing with. So, you're probably getting the same content repeated in each section of your table. (It'd help if you told us exactly what the problem is, though.)

Display certain numbers of row at one time, click Button, display more

i m quite stucking with this problem. i have a NSArray *list holding 70 elements from a plist file. now i need to populate them to UITableView.
what i want is to display 20 items at a time, then click the 21st cell to display another 20.. then press the 41st cell to display another 20 items... and so on till all 72 cells reveal..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
static NSString *addCellID = #"addMoreCell";
UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:addCellID];
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"labelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
} else if (indexPath.row < 25){
NSDictionary *dic = [self.list objectAtIndex:indexPath.row];
cell.textLabel.text = [dic objectForKey:#"Title"];
cell.detailTextLabel.text = [dic objectForKey:#"Tips"];
return cell;
} else {
if (cell == nil) {
addCell.textLabel.text = #"Load More...";
return cell;
}
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 20)
{
[self.tableView reloadData];
}
}
Take an array, in which you will add 20 elements from plist, and
set numberOfRowsInSection = [array count] and
check if numberOfRowsInSection < totalNumberOfRecords in plist, if it is less, then increment numberOfRowsInSection by 1.
And in cellForRowAtIndexPath
check if (indexPath.row < totalNumberOfRecords -2 && indexPath.row < numberOfRowsInSection-1)
then add "LoadMore Cell" else not.
So I do something quite similar. You have an array with 70 strings (or dictionaries) to show in the table. The 'numberOfRowsInSection:' should be using an ivar which is initially set to 20.
When your button gets pressed, you increase the ivar by 20 (but to no more than the actual amount of data!), you reload the table, then you can scroll the table up to the "new" cell using tableView methods.

uiwebview in every tableviewcells

I need to display each webview in each of the tableviewcell in my Uitableview.When using the below code,when there are 2 elements,the first cell is empty but second cell is correct.hrs and hrsHtml contains all values,the problem is only the last data is displaying in their appropriate cell in tableview.Other cells are just blank.Also is total cells is 2,first we can only been able to see the 2nd cell only,but after scrolling tableview reloads and 2nd cell disappears and 1st cell gets displayed.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [brId count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:hrs];
hrsHtml = [NSString stringWithFormat:#" <font size=\"2\" face=\"Arial\">%# </font>",[html objectAtIndex:indexPath.row]];
[hrs loadHTMLString:hrsHtml baseURL:nil];
return cell;
}
Screenshot when tableview appears,Only webview in cell 2
Screenshot when tableview scrolls,Only webview in cell 1,cell 2 disappears
Since hrs and hrsHtml are single objects, you only have one of each even though you have multiple cells. If you modify hrs, it will change for all cells since they seem to be sharing it. (Unless you have other code somewhere that changes the objects that those variables point to.)
Another odd thing is that you use a brId array to determine the number of rows and a html array to get the row content. If those ever get out of synchronization, you will have problems.
Also, you should only add a subview to a cell when you create a new one.
- (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:nil] ;
}
hrs = [[UIWebView alloc] initWithFrame:CGRectMake(10,0,320,84)];
hrs.userInteractionEnabled = YES;
hrs.backgroundColor = [UIColor clearColor];
hrsHtml = [NSString stringWithFormat:#" <font size=\"2\" face=\"Arial\">%# </font>",[html objectAtIndex:indexPath.row]];
[hrs loadHTMLString:hrsHtml baseURL:nil];
[cell.contentView addSubview:hrs];
hrsHtml = nil;
return cell;
}
Now the webview loads correctly in every tableviewcell.
i think you have problem in initializing cell try below code
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}

TableView returns null (0) when selecting row

I have a TableView control which I've sectioned/grouped. However this has now broken the code which determines the item I've selected in the Table.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
*/
//detailViewController.detailItem = [NSString stringWithFormat:#"Row %d", indexPath.row];
if (_delegate != nil) {
Condition *condition = (Condition *) [_conditions objectAtIndex:indexPath.row];
[_delegate conditionSelectionChanged:condition];
}
}
My sample data currently has 6 sections, the first one contains two items with all the rest containing one. If I select the second item in the first section, the details view updates fine, any other selection always shows the first item in the first section (item 0).
I'm assuming this is because indexPath.row is returning 0 for the other sections as it's the first item in that section, which is then causing the first object from my array to be chosen.
How do I fix my code to ensure the correct item is selected? Everything else works fine other than this.
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
//get the letter in the current section
NSString *alphabet = [conditionIndex objectAtIndex:[indexPath section]];
//get all conditions beginning with the letter
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name beginswith[c] %#", alphabet];
NSArray *conditionsGrouped = [_conditions filteredArrayUsingPredicate:predicate];
if ([conditionsGrouped count] > 0) {
Condition *condition = [conditionsGrouped objectAtIndex:indexPath.row];
cell.textLabel.text = condition.name;
}
return cell;
}
You should take into account indexPath.section, as well.
You should create a bi-dimensional _conditions array, indexed by section and then by row.
Got it working by following the answer at this post:
UITableView Problem with Sections