UIImage in UITableviewcell layout - uitableview

I have a problem layout the objects in the code.
Here is a picture of current and desired status:
And here is the code
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"My Favorites", #"My Favorites");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
favoritesTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 364) style:UITableViewStylePlain];
[favoritesTable setHidden:NO];
[favoritesTable setDelegate:self];
[favoritesTable setDataSource:self];
[self.view addSubview:favoritesTable];
[favoritesTable release];
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"<#MyCell#>";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
NSInteger objectLocation = indexPath.row;
FoodData* food = (FoodData*)[resultsArray objectAtIndex:objectLocation];
cell.imageView.image = [UIImage imageNamed:#"paris.jpg"];
cell.imageView.frame = CGRectMake(270, 4, 40, 36);
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UILabel* lblText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 20)];
UILabel* lblType = [[UILabel alloc] initWithFrame:CGRectMake(0, 21, 260, 20)];
[lblText setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
[lblType setFont:[UIFont fontWithName:#"Helvetica" size:9.0]];
lblType.textColor = [UIColor blueColor ];
[lblText setText:[food foodName]];
[lblType setText:[food foodType]];
[cell addSubview:lblText];
[cell addSubview:lblType];
[lblText release];
[lblType release];
return cell;
}

on ios5, making use of the prototype cell on the IB will be the easiest way out for you.
if you are not on ios 5, i do highly recommending using a custom tableviewcell.
The short solution to your issue can be found here in the link below.
UITableViewCell with image on the right?

Related

TableViewCell is piled up and appear again and again

I implemented TableView with ADLively TableView.
But if I scroll tableView, the cell's texts become to be piled up again and again....
Using "cell.textLabel" is good, adding UILabel is not good than that but if I use "cell.textLabel", I can't resize width of textLabel.
(I want to add UIImageView on the left and right side of text)
So now, I use the way to add UILabel.
What should I do?
Here is the code.
- (void)viewDidLoad {
CGRect rect = CGRectMake(0, 50, 320, self.view.frame.size.height-150);
self.tableView = [[ADLivelyTableView alloc]initWithFrame:rect style:UITableViewStylePlain];
self.tableView = (ADLivelyTableView *)self.tableView;
self.tableView.initialCellTransformBlock = ADLivelyTransformFan;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview: self.tableView];
//self.tableView.backgroundColor = [UIColor blueColor];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.section count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];
if (cell == nil){
myCellView = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"];
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)];
tableTitleLabel.text = [set objectAtIndex:0];
[tableTitleLabel setNumberOfLines:0];
tableTitleLabel.backgroundColor = [UIColor clearColor];
tableTitleLabel.textColor = [UIColor blackColor];
tableTitleLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
tableTitleLabel.adjustsFontSizeToFitWidth = YES;
tableTitleLabel.minimumScaleFactor = 10.0f;
[myCellView.contentView addSubview:tableTitleLabel];
}
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
[(UILabel *)[cell.contentView viewWithTag:1] setText:[set objectAtIndex:0]];
return cell;
}
- (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)];
tableTitleLabel.text = [set objectAtIndex:0];
[tableTitleLabel setNumberOfLines:0];
tableTitleLabel.backgroundColor = [UIColor clearColor];
tableTitleLabel.textColor = [UIColor blackColor];
tableTitleLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
tableTitleLabel.adjustsFontSizeToFitWidth = YES;
tableTitleLabel.minimumScaleFactor = 10.0f;
[cell.contentView addSubview:tableTitleLabel];
}
It is not a good idea to keep adding subviews in cell on scroll but if you do not want to change the code that you have already written, use the following to remove all subviews of that cell before you call you updateCell method.
for (UIView *view in [cell subviews])
{
[view removeFromSuperview];
}

How to change TableList UILabel backGroundColor when we tapped on UIbutoon?

Hi I am very new in iOS and I have created one tablelist on my Viewcontroller and here on my tablelist row I have added one UIlabel programatically, ok that's fine.
And I have added one UIbutton programatically on my "self.view".
Here my main requirement is when I click that button, I want to change UIlabel background color which was added on my tablelist row.
For this I have tried the code bellow, but UIlabel backgroudcolor is not changing.
Please help me.
my code:-
#import "ViewController.h"
#interface ViewController (){
UILabel *label;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (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];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
label.backgroundColor = [UIColor redcolor];
}
Your question was not clear if you want to change the label color on all cell or just some.
Anyway this color change must be made cellForRowAtIndexPath which is where the update / creation of the cell is made.
You can create a variable to inform if you want the color change or not and it check this on cellForRowAtIndexPath.
#implementation ViewController {
UITableView *tableView;
bool changeColor;
}
-(void)Method:(id)sender{
changeColor != changeColor;
[tableview reloadData];
}
- (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];
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
[cell.contentView addSubview:label];
}
if (changeColor){
label.backgroundColor = [UIColor redcolor];
}else{
label.backgroundColor = [UIColor clearColor];
}
return cell;
}
if you want change especific labels, you can create a nsarray to control what cell will be change
#interface ViewController (){
UILabel *label;
NSMutableArray *lableArray;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
lableArray=#[].mutableCopy;
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (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];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[lableArray addObject:label];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
//
// label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
// label.backgroundColor = [UIColor clearColor];
// [cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
[lableArray makeObjectsPerformSelector:#selector(setBackground:) withObject:[UIColor redColor]];
// label.backgroundColor = [UIColor redcolor];
}
cell is Reusable so creation should put in cell == nil condition,at every run into the condition will creation a new UILabel ,if you want change all of them ,you should put them in a collection ,such as NSMutableArray...
import "ViewController.h"
#interface ViewController ()
{
UILabel *label;
BOOL isTouchButton;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (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];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
label.tag = indexPath.row;
if(!isTouchButton)
{
cell.labelUserName.backgroundColor = [UIColor clearColor];
}
else{
cell.labelUserName.backgroundColor = [UIColor redColor];
}
[cell.contentView addSubview:label];
return cell;
}
//button action
-(void)Method:(id)sender
{
if(!isTouchButton)
{
[tablelistCreation reloadData];
isTouchButton = YES;
}
else{
[tablelistCreation reloadData];
isTouchButton = NO;
}
}

On footer button click table view does not reload the contents of table?

On table view footer i have one button. Clicking footer button will reload the table data. But when the data is reloaded the data of table is shown again in which I have cameraBtn to select the image from UIImagePicker view controller and display it on image view. But I want when the table is reloaded the images of table could not be shown. Anyone can help me out for this. I am using the below code.
.h file
{
BOOL isBtnClicked; // in .h file
}
.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *subviewArray = cell.contentView.subviews;
for (UIView *view in subviewArray)
{
[view removeFromSuperview];
}
UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)];
picLabels.text = listItems[indexPath.row];
picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
picLabels.font =[UIFont systemFontOfSize:13];
[cell.contentView addSubview:picLabels];
if (isBtnClicked )
{
imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
imageParcel.userInteractionEnabled=YES;
imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
imageParcel.layer.cornerRadius = 30;
imageParcel.layer.masksToBounds = YES;
[cell.contentView addSubview:imageParcel];
}
else
{
imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
imageParcel.userInteractionEnabled=YES;
imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
imageParcel.image = [imageArray objectAtIndex:indexPath.row];
imageParcel.layer.cornerRadius = 30;
imageParcel.layer.masksToBounds = YES;
[cell.contentView addSubview:imageParcel];
}
parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
parcelButton.frame = CGRectMake(180, 0, 50, 50);
[parcelButton setBackgroundImage:[UIImage imageNamed:#"Camera.png"]forState:UIControlStateNormal];
parcelButton.tag = indexPath.row;
[parcelButton addTarget:self action:#selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = parcelButton;
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
mainview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[mainview setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
createDelvTableView.tableFooterView = mainview;
UIButton *addSection = [[UIButton alloc]initWithFrame:CGRectMake(270, 0, 40, 40)];
isBtnClicked = YES;
[addSection setBackgroundImage:[UIImage imageNamed:#"addSection.png"] forState:UIControlStateNormal];
[addSection addTarget:self action:#selector(addSectionAction) forControlEvents:UIControlEventTouchUpInside];
[mainview addSubview:addSection];
return mainview;
}
- (void)addPictureAction:(UIButton*)image
{
NSLog(#"Add Pictures");
isBtnClicked = NO;
index=image.tag;
// if ([UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.allowsEditing = YES;
photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:photoPicker animated:YES completion:NULL];
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
- (void)addSectionAction
{
NSLog(#"ADD section");
indexes++;
isBtnClicked = YES;
[createDelvTableView reloadData];
}
Thanks in advance.
I can't understand why you add object on cell again and again.. then remove them.. Modify your cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)];
picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
picLabels.font =[UIFont systemFontOfSize:13];
picLabels.tag = 101;
[cell.contentView addSubview:picLabels];
UIImageView *imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
imageParcel.tag = 102;
imageParcel.userInteractionEnabled=YES;
imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
imageParcel.layer.cornerRadius = 30;
imageParcel.layer.masksToBounds = YES;
[cell.contentView addSubview:imageParcel];
UIButton *parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
parcelButton.frame = CGRectMake(180, 0, 50, 50);
[parcelButton setBackgroundImage:[UIImage imageNamed:#"Camera.png"]forState:UIControlStateNormal];
parcelButton.tag = indexPath.row;
[parcelButton addTarget:self action:#selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];
}
UILabel *lbl = (UILabel*)[cell.contentView viewWithTag:101];
picLabels.text = listItems[indexPath.row];
UIImageView *img = (UIImageView*)[cell.contentView viewWithTag:102];
if (!isBtnClicked )
{
imageParcel.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
}
else
{
imageParcel.image = [UIImage new];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = parcelButton;
return cell;
}
Remove isBtnClicked = YES; from viewForFooterInSection
On footer's button action:
isBtnClicked = YES;
[yourTblView reloadData];
On ImagePicker button action:-
isBtnClicked = NO;

How to load elements of table view in background and refresh it, when they have downloaded?

In my application i have a table view, that have a UIImageView in every cell. It brings pictures from the internet, and of course I want it in my application, and not waiting until all pictures will be loaded and saved in CoreData. I want to show textual information first, and that show pictures when they are ready.
First this is tableView delegates:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return maxSerials;
//return [[self.fetchedResultsController fetchedObjects] count];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:0];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, customView.frame.size.width, 30)];
lineView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:0.90];
[customView addSubview:lineView];
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:customView.frame];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor colorWithWhite:0 alpha:1];
sectionHeader.text = [[serialsAnswer objectAtIndex: section] objectForKey: #"name"];
[customView addSubview:sectionHeader];
return customView;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"serialCell"];
NSLog(#"cell");
Serial *serial = [self.fetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];
UIImage *poster = [[UIImage alloc] initWithData: serial.poster];
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 160, 232)];
imageView.image = poster;
[cell addSubview:imageView];
UILabel *views = [[UILabel alloc] initWithFrame: CGRectMake(170, 22, 140, 15)];
views.textAlignment = NSTextAlignmentCenter;
views.text = #"Просмотров:";
UILabel *views2 = [[UILabel alloc] initWithFrame: CGRectMake(170, 42, 140, 15)];
views2.textAlignment = NSTextAlignmentCenter;
views2.text = [[serialsAnswer objectAtIndex: indexPath.section] objectForKey: #"views"];
UILabel *IMDB = [[UILabel alloc] initWithFrame: CGRectMake(170, 99, 140, 15)];
IMDB.textAlignment = NSTextAlignmentCenter;
IMDB.text = #"IMDB:";
UILabel *IMDB2 = [[UILabel alloc] initWithFrame: CGRectMake(170, 119, 140, 15)];
IMDB2.textAlignment = NSTextAlignmentCenter;
IMDB2.text = [[serialsAnswer objectAtIndex: indexPath.section] objectForKey: #"imdb_rating"];
UILabel *year = [[UILabel alloc] initWithFrame: CGRectMake(170, 176, 140, 15)];
year.textAlignment = NSTextAlignmentCenter;
year.text = #"Год:";
UILabel *year2 = [[UILabel alloc] initWithFrame: CGRectMake(170, 196, 140, 15)];
year2.textAlignment = NSTextAlignmentCenter;
year2.text = [[serialsAnswer objectAtIndex: indexPath.section] objectForKey: #"year"];
[cell addSubview: year];
[cell addSubview: year2];
[cell addSubview: IMDB];
[cell addSubview: IMDB2];
[cell addSubview: views];
[cell addSubview: views2];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 240.0f;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:true];
[self performSegueWithIdentifier: #"serialsToSerial" sender: indexPath];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == maxSerials - 1)
{
maxSerials +=10;
[self downloadCells];
}
}
This is save method:
- (void) saveSerial:(NSDictionary *) newSerial
{
id path = [[NSString alloc] initWithFormat:#"http://sakh.tv/%#", [newSerial objectForKey: #"poster"]];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
_serial.poster = data;
_serial = [NSEntityDescription insertNewObjectForEntityForName:#"Serial" inManagedObjectContext:[self managedObjectContext]];
_serial.serial_id = [newSerial objectForKey:#"id"];
_serial.views = [[NSNumber alloc] initWithInt: [[newSerial objectForKey:#"views"] integerValue]];
_serial.name = [newSerial objectForKey:#"name"];
[super saveToCoreData];
}
How can I make my table view dynamic?
Since you are using fetchedResultController for your table, implement (void)controllerDidChangeContent:(NSFetchedResultsController *)controller and reload your table view by calling [self.tableView reloadData]
By the way, seems like your are not subclassing tableViewCell, consider this.
Instead of using:
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"serialCell"];
Serial *serial = [self.fetchedResultsController.fetchedObjects
objectAtIndex:indexPath.section];
try:
MyCell *aCell = (MyCell *) [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Custom UITableViewCell functions are called, but native table is presented

Custom UITableViewCell functions are called, but native table is presented
This is my code in the init function:
self.answersTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, 280, 500)];
self.answersTable.dataSource = self;
self.answersTable.delegate = self;
This is my code in the viewDidLoad function:
[self.answersTable registerClass:[AnswerTableViewCell class]
forCellReuseIdentifier:CellIdentifier];
This is my table delegate functions, which are called, and the cell is not nil in the end of it:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.question.answers count];
}
-(AnswerTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AnswerObject* answer = self.question.answers[indexPath.row];
[cell setupAnswerTableViewCell:self.question answer:answer row:indexPath.row];
return cell;
}
And here is AnswerTableViewCell:
#import "AnswerTableViewCell.h"
#implementation AnswerTableViewCell
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
-(void)setupAnswerTableViewCell:(QuestionObject*)question
answer:(AnswerObject*)answer
row:(NSInteger)row{
self.question = question;
self.answer = answer;
self.row = row;
self.answerLabel.text = answer.answerText;
[self.answerToggle addTarget:self
action:#selector(flip:)
forControlEvents:UIControlEventTouchDown];
self.answerToggle.tag = [answer.answerID intValue];
if (self.row == 0) {
self.backgroundImage.image = [UIImage imageNamed:#"List_Top_Item_Not_Selected_612x113px.png"];
}
else if (self.row == ([self.question.answers count] - 1)){
self.backgroundImage.image = [UIImage imageNamed:#"List_Bottom_Item_Not_Selected_612x113px.png"];
}
else{
self.backgroundImage.image = [UIImage imageNamed:#"List_Item_Not_Selected_612x113px.png"];
}
}
So, whats wrong?
The problem is:
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
specifically: UITableViewCellStyleDefault
You're asking it to create a Cell with the default style. Either just use [[AnswerTableViewCell alloc] init]; and set the reuse identifier. Or create your own init that takes in a reuse identifier without a style param
EDIT:
After the Cell code was added I believe the issue to be that you are using the init function to add your UI elements. These are most likely being overridden by the cells UIView contentView. Its the contentView you need to customise in order to add these elements.
This article will walk you through how to do this and using the drawRect method to customise the UIView.
http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/
As stupid as it may sound, the thing that fixed it for me was:
self.answersTable.backgroundColor = [UIColor clearColor];

Resources