how to customise UITableView? - ios

How to add image and texts as shown in the below snapshots?I am already using navigation controller in my application, so is there any problem in including UITableView controller as a new navigated page from home page.

when configuring your UITableViewCellyou can add a UIImageView and UILabel to the cells contentView.
UITableViewCell *cell = ...;
UIImageView *yourImageView = ...;
UILabel *yourLabel = ...;
[cell.contentView addSubview:yourImageView];
[cell.contentView addSubview:yourLabel];

I would subclass the UITableView Cell and create my own cell MyCustomTableViewCell, with the UIImageView and UILabel inside.
Then you could do this to update your cells in reusing case
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
MyCustomTableViewCell *myCell = (MyCustomTableViewCell)cell;
myCell.label.text = newText;
myCell.imageView.image = newImage;
return cell;
}

Related

How to change the selected cell background color without missing the text

I want to change the selected cell background color and keep the text on that cell. Then I did like this, it can change the cell color but the text on the cell will disappear.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.receivedData objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIView *bgColorView = [[UIView alloc]init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];
cell.selectedBackgroundView = bgColorView;
}
You are replacing the current view (the one that has got your text in it) with a new instance of UIView (with no text in it, but your new color). That is why your text is disappearing. You should change the background color property of selectedBackgroundview like so:
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];
Although I think you can even ditch the selectedBackgroundView part and just do cell.backgroundColor = ... in didSelectRowAtIndexPath

Custom UItableViewCell label is overlapping

Using UITableView and found that cells are not dequeuing properly.I have created prototype cells using storyboard,and have UIlabel over cell.
- (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];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Here is the image of UITableView which i am getting when i scroll the TableView
This could simply be a minor mistake. Try using a different tag value, or verify the values being used in the prototype cell.
You're not using your custom cell in CellForRowAtIndexPath. Code looks fine just inseatd of UITableViewCell dequeue your custom cell like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell"; //needs to be the identifier you defined in story boards
YourCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Then you need to create a subclass of UITableViewCell and name it YourCustomCell and link all IBOutlet objects like labels etc. put this line of code on top of your .h file #import YourCustomCell.h and simply access all properties of that cell like cell.textLabel instead of createting label and casting it to view with tag.
I fixed this issue by using "nil" instead of cellIdentifier
eg:
Instead of
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Use this
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];
It did the trick for me
You just need to tick the "clears graphic context" property of the label in storyboard.

SDWebImage UIImageView+WebCache in custom UIImageView

I'm having a problem with loading images from my webserver into my UIImageView in a tableview
This code works perfect, but the images are placed weird and ugly in my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell. Note that this name is the same as in the storyboard
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Set the correct name in the cell.
//Do so by looking up the row in indexpath and choosing the same element in the array
NSInteger currentRow = indexPath.row;
Image* currentImage = [self.images objectAtIndex:currentRow];
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:#"testimage.jpg"]];
return cell;
}
So I created an UIImageView on my storyboard, and changed the code to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell. Note that this name is the same as in the storyboard
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Set the correct name in the cell.
//Do so by looking up the row in indexpath and choosing the same element in the array
NSInteger currentRow = indexPath.row;
Image* currentImage = [self.images objectAtIndex:currentRow];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
// Here we use the new provided setImageWithURL: method to load the web image
[imgView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:#"testimage.jpg"]];
return cell;
}
But if I run this code on my device, I'm getting a signal SIGABRT error.
How can I get this working on my custom Imageview?
In the first code snippet, add these lines:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
You are getting SIGABRT because your imagevIview is NOT added to cell, but cell's contentView..! So the right way to get the imageView should be,
UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];

How to draw image on the right side of the UITableView cell on iOS 7 on iPad?

How to draw image on the right side of the UITableView cell on iOS 7 on iPad?
Tried and don't help the following code:
tableView.contentInset = UIEdgeInsetsZero;
[cell contentView].frame = CGRectMake(0, [cell contentView].frame.origin.y, cell.frame.size.width, [cell contentView].frame.size.height);
You most likely want to set the accessoryView property on UITableViewCell. This will put a an image view on the right side of a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Configure cell
}
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"<some image name>.png"]];
cell.accessoryView = image;
}

Creating more detailed Table Cells

I want to create more detailed table cells. An example I found is the reddit.com client AlienBlue. Each cell has an image, the title of the post, then below it four other pieces of text. How can I "layer" text in a table cell?
Hi you should subclass UITableViewCell for custom User interface of a cell in a table view.
Also look at: UITableViewDataSource and UITableViewDelegate for other options with regard to table view.
Probably your subclassed UiTableViewCell will have custom fields for title (UILabel), caption (UILabel) and for an image (UIImageView)
Examples:
Crafting Custom UITableView Cells
Customize Table View Cells For UITableView
You will want to create a custom UITableViewCell. It's quite trivial, and can be done either entirely in code (my favorite) or through IB.
Google custom UITableView cell for some great tutorials.
Use NSMutableArray of NSMUtableDictionary, as opposed to NSArray or NSMutableArray, to hold data (strings, images, ...).
Set the table cell style property to UITableViewCellStyleSubtitle so that the table can display the detailedTextLabel (cell.detailTextLabel.text).
You can set an image to the table cell.
Example
- (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];
}
// Configure the cell...
UIImage *image = [[list1 objectAtIndex:indexPath.row] objectForKey:key1a];
cell.imageView.image = image;
NSString *categoryname = [[list1 objectAtIndex:indexPath.row] objectForKey:key1b];
cell.textLabel.text = categoryname;
cell.detailTextLabel.text = [[list1 objectAtIndex:indexPath.row] objectForKey:key1e];
cell.detailTextLabel.textColor = [UIColor blueColor];
return cell;
}
- (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];
}
cell.textLabel.text=[NSString stringWithFormat:#"%#",[appdelegate.name objectAtIndex:indexPath.row]];
UIImage *image = [[array1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [[array1 objectAtIndex:indexPath.row];
return cell;
}

Resources