How to configure UITableViewCell programmatically? - objective-c

I am trying to create a UITableView from code (never done it this way before) which will be displayed in a UIPopover. I want to list the contents (filenames only) of a directory. This is my code:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"FilenameCell";
UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel = fileList[0];
return cell;
}
It's obviously not working, I'm getting an error: "Assignment to readonly property". So, the question is: How do I set the cell's data in this case?

You need to set your textLabel's text property (which I'm sure was just a typo in your case):
cell.textLabel.text = fileList[0];

Related

How to edit UITableViewCell frame position programmatically

I need to edit the position of an UITableViewCell programmatically, this is the relevant code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
if([[[self.arForTable objectAtIndex:indexPath.row] valueForKey:#"level"] intValue] == 1)
{
CGRect frame = cell.textLabel.frame;
frame.origin.x= 30;
cell.textLabel.frame= frame;
}
return cell;
}
Using breakpoints I can see that the if condition works but the code to edit the frame position does nothing. Is it possible to edit the position like this or i'll have to subclass UITableViewCell?
I think you should create 2 subclasses of UITableViewCell with 2 different identifiers depending on the one you want to display if they are different.
You could customize frame in each subclass as you want and it will be easier for you to work with it

Property not found on object of type 'UITableViewCell *'

I am getting this error while working on my xcode project
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"DateTimeCell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"cell" ];
}
NSString *sysDeliveryType=[[jsonData objectAtIndex:indexPath.row]valueForKey:#"sysord_DeliveryType"];
NSString *sysExpDeliveryTime=[[jsonData objectAtIndex:indexPath.row]valueForKey:#"sysord_ExpectedDeliveryTime"];
NSString *sysOrderDateTime=[[jsonData objectAtIndex:indexPath.row]valueForKey:#"sysord_OrderDateTime"];
NSLog(#"%#",sysDeliveryType);
cell.lblDeliveryPickUP.text=sysDeliveryType;
cell.lblexpDeliveryTime.text=sysExpDeliveryTime;
cell.lblsysOrderPlacedTime.text=sysOrderDateTime;
return cell;
}
Also outlate are given properly but this Error Of property not found display in my Code
You need to cast the cell to DateTimeCell.
For example: [[(DateTimeCell *)cell lblDeliveryPickUP] setText:sysDeliveryType];
The labels you try to set there are not properties of UITableViewCell.
Declare the cell to be the desired type, and supply the correct identifier to the dequeue method.
DateTimeCell1 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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.

Can't set cells in UITableView from NSMutableArray getting EXC_BAD_ACCESS

Can anyone help I am trying to set the values of the cells in a table view but I am getting EXC_BAD_ACCESS. here is my code :
- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView depueueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [webRef objectAtIndec:indexPath.row];
return cell;
}
I am trying to set the values from an NSMutableArray. There are objects in the array I can print them to the console but not into the cells.
I have solved this. I was not retaining anything when declaring the Arrays. Thanks everyone for the help you have provided.

Three20 UITableViewCellStyleValue1

How do I create an item in my datasource using a cell that is UITableViewCellStyleValue1? Do I have to create my own custom cell to do it? I don't see anything in the sample catalog.
thank,
howie
A new table item cell was added that emulates the table settings cell style. see https://github.com/facebook/three20/pull/682
You can either merge it manually or wait for the next three20 release which will hopefully include this change.
The point of using UITableViewCellStyleValue1 is that your using a pre-defined layout, so no you don't use a custom layout.
Example:
// 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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"SettingLabel";
cell.detailTextLabel.text = #"SettingValue";
return cell;
}