Implementing UIWebView on a custom UITableViewCell - objective-c

I have this UIWebView inside a custom UITableViewCell that I've created.
The problem is, when I implement the UIWebView in "MyMain", it's working but when I scroll the text is readded again and again on over the cells.
When I implement the UIWebView in the custom cell class itself it's not showing it at all.
UITableViewCustomCell.h
#property (nonatomic, weak) IBOutlet UIWebView *wvMainText;
#property (nonatomic, strong) NSString *wvTitle;
#property (nonatomic, strong) NSString *wvPrice;
UITableViewCustomCell.m
#synthesize wvMainText = _wvMainText;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// THIS IS WHERE I IMPLEMENT THE WEBVIEW?
}
return self;
}
MyMain
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myListCell";
UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"UITableViewCustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (UITableViewCustomCell *)view;
}
}
}
cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"title"];
cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"price"];
wv.scrollView.scrollEnabled = NO;
NSString *htmlString = [NSString stringWithFormat:
#"<html><body>%#,%#</body></html>", self.wvTitle, self.wvPrice];
[wv loadHTMLString:htmlString baseURL:nil];
[wv setBackgroundColor:[UIColor clearColor]];
[wv setOpaque:NO];
return cell;
}

Try this...It may solve your issue.
In your cellForRowAtIndexPath method please do changes like this....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myListCell";
UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"UITableViewCustomCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (UITableViewCustomCell *)view;
}
}
cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"title"];
cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:#"price"];
wv.scrollView.scrollEnabled = NO;
NSString *htmlString = [NSString stringWithFormat:
#"<html><body>%#,%#</body></html>", self.wvTitle, self.wvPrice];
[wv loadHTMLString:htmlString baseURL:nil];
[wv setBackgroundColor:[UIColor clearColor]];
[wv setOpaque:NO];
}
return cell;
}

Related

Data is unexpectadely reset back to 0 when passed to a new View Controller

My lensInt(an integer) get reset back to 0 when i pass lensInt =1 to my ViewController1( This is a table view) at from MenuViewController. My Menu View Controller is a facebook like sliding background . Also, My ViewController1 is embeded in a Navigation Controller Bar. The Integer Only reset when it passes the #implementation TableViewController(I uses breakpoint to debug it).
My ViewController1.h:
#import <UIKit/UIKit.h>
#interface ViewController1 : UITableViewController{
NSMutableArray *ACUVUE;
NSMutableArray *BAUSCH_LOMB;
int lensInt;
}
My ViewController1.m:
#import "ViewController1.h"
#implementation ViewController1
#synthesize lensInt;
- (void)viewDidLoad
{
ACUVUE =[[NSMutableArray alloc]
initWithObjects:#"1-DAY ACUVUE DEFINE",#"1-DAY ACUVUE ASTIGMATISM", nil];
BAUSCH_LOMB=[[NSMutableArray alloc]
initWithObjects:#"B+L PURE VISION",#"B+L PURE VISION MULTIFOCAL",#"B+L PURE VISION TORIC",nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.lensInt == 0){
return [ACUVUE count];}
if(self.lensInt == 1){
return [BAUSCH_LOMB count];}
[self.tableView reloadData];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }
// Configure the cell...
if(self.lensInt == 0){
cell.textLabel.text = [ACUVUE objectAtIndex:indexPath.row];}
if(self.lensInt == 1){
cell.textLabel.text = [BAUSCH_LOMB objectAtIndex:indexPath.row];}
//---------- CELL BACKGROUND IMAGE -----------------------------
UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image = [UIImage imageNamed:#"LightGrey.png"];
imageView.image = image;
cell.backgroundView = imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
//Arrow
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;}
#end
My MenuViewController.m:
#import "MenuViewController.h"
#import "ECSlidingViewController.h"
#import "ViewController1.h"
#interface MenuViewController ()
#property (strong, nonatomic) NSArray *menu;
#property (strong, nonatomic) NSArray *section1;
#property (strong, nonatomic) NSArray *section2;
#property (strong, nonatomic) ViewController1 *lens;
#end
#implementation MenuViewController
#synthesize lens;
#synthesize menu, section1, section2;
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
tableView.backgroundColor = [UIColor darkGrayColor];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textColor =[UIColor whiteColor];
tableView.separatorColor =[UIColor blackColor];}
- (void)viewDidLoad
{ [super viewDidLoad];
self.section1 = [NSArray arrayWithObjects:#"Acuvue", #"Bausch + Lomb", nil];
self.section2 = [NSArray arrayWithObjects:#"Daily", #"Monthly", nil];
self.menu = [NSArray arrayWithObjects:self.section1, self.section2, nil];
[self.slidingViewController setAnchorRightRevealAmount:190.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.menu count]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return [self.section1 count];
} else if (section == 1) {
return [self.section2 count];
}}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Contact Lens Brand";
} else if (section == 1) {
return #"Contact Lens Type";
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; }
if (indexPath.section == 0) {
cell.textLabel.text = [NSString stringWithFormat:#"%#", [self.section1 objectAtIndex:indexPath.row]];
} else if (indexPath.section == 1) {
cell.textLabel.text = [NSString stringWithFormat:#"%#", [self.section2 objectAtIndex:indexPath.row]];
}
return cell;}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIViewController *newTopViewController;
if (indexPath.section == 0) {
if([[section1 objectAtIndex:indexPath.row] isEqual:#"Acuvue"]){
lens.lensInt= 0 ;
}
if([[section1 objectAtIndex:indexPath.row]isEqual:#"Bausch + Lomb"]){
lens.lensInt = 1 ;
}
NSString *identifier = [NSString stringWithFormat:#"product"];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
} else if (indexPath.section == 1) {
NSString *identifier = [NSString stringWithFormat:#"product"];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];}];
}
#end
you should pass the data between views using prepareForSegue if you are using storyboards.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass object to your view controller here
[vc setMyObjectHere:object];
}
}

populate uitableview as subview of uicollectionview

I have a problem in populating a uitableview created programmatically as a subview of uicollectionview.
here's my code:
.h file
#import <UIKit/UIKit.h>
#interface TheViewController : UICollectionViewController<UITableViewDataSource>
#property (atomic,strong) IBOutlet UIBarButtonItem *plus;
-(IBAction) addTeamPressed:(id)sender;
#end
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *jsonParsingError = nil;
teamsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.window.bounds.size.width, self.view.window.bounds.size.height)];
NSData *t = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."] options:NSDataReadingMappedIfSafe error:&jsonParsingError];
NSArray *tmp = [NSJSONSerialization JSONObjectWithData:t options:0 error:&jsonParsingError];
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
teams = [[NSMutableArray alloc]init];
[teamsView addSubview:teamCell];
teamsView.delegate = self;
teamsView.dataSource = self;
for(int i =0;i<tmp.count;i++){
[teams addObject:[tmp objectAtIndex:i]];
}
plus.title = #"Done";
[self.view addSubview:teamsView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
// If You have only one(1) section, return 1, otherwise you must handle sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [teams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
my tableview is displaying correctly, of course thanks to other functions not mentioned here, but no rows in the subview.
the compiler prints out the number of teams, so it calls the delegate methods, but it doesn't populate the subview...
what 's wrong with this?
thanks in advance
Dario
First you should remove this from your viewDidLoad:
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[teamsView addSubview:teamCell];
Then make sure that your teams array is retained, just put this line to the header file:
#property (nonatomic, strong) NSMutableArray* teams;
And then use self.teams instead of just teams
At the end of viewDidLoad method you should call [teamsView reloadData];
Also it would be good to change your table data source method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// please make sure that you allocate required object here
cell = [[TeamsNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// please make sure that you do have `name` property in your `TeamsNameCell` class
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
Please make sure that your TeamsNameCell class has name property in the header:
#property(nonatomic, strong) UILabel* name;
And also do not forget to allocate this label in the constructor of TeamsNameCell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,50)];
[self addSubview:self.name];
}
return self;
}
Or Maybe it is better to use default cell's label:
instead of cell.name.text = #"text"; use cell.textLabel.text = #"text";
solution is,
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.labelText.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;

UITableView in UIViewController - Connecting cells to UIViewController

I have a UITableView in a UIViewController. I have other UIViewControllers created in Storyboard that I want to connect to my cells.
How can I connect UIViewControllers created in Storyboard to my cells?
MyViewController.h
#interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *myTableView;
}
#property (nonatomic, retain) UITableView *myTableView;
MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:#"CELL1", #"CELL2 & CELL3", nil];
self.listData = array;
[array release];
[myTableView setDataSource:self];
[myTableView setDelegate:self];
[[self view] addSubview:myTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UpdatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"UpdatesTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (UpdatesTableViewCell*)view;
}
}
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
You can either create segues from this controller to the other ones and then choose which segue to perform when someone taps a cell or you can use a reference to the storyboard to call instantiateViewControllerWithIdentifier: and make that controller active on a tap.

customising UITableViewCell with picture objective-C

I want to have custom cell in UITableView, I create my UIView via storyboard and I linked them to the code
I don't know why my picture does not appear
Would you please check my code ?
customise code .h
: UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *weekImg;
#end
customise code .m
#synthesize weekImg;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
weekImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"red.png"]];
}
return self;
}
My method: cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"WeekTableViewCell"];
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
}
I think this is because your code is just looking for previosuly created cells. at th start no cells will be created and wont be able to retrieve any this way.
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"WeekTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WeekTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[WeekTableViewCell class]])
{
cell = (WeekTableViewCell *)currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
this way create a new cell if there isnt a cell to re use
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];
cell.textLabel.text=[NSMUtableArray objectAtIndex:indexPath.row];
switch (indexPath.row)
{
case 0:
cell.imageView.image=[UIImage imageNamed:#"img1.png"];
break;
case 1:
cell.imageView.image=[UIImage imageNamed:#"img2.png"];
break;
case 2:
cell.imageView.image=[UIImage imageNamed:#"img3.png"];
break;
case 3:
cell.imageView.image=[UIImage imageNamed:#"img4.png"];
break;
default:
break;
}
}
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];, it is used by QuartzCore.
The output of the cell like this, we can adjust the image size.

Table check box just for one section objective-c

I have a uiTableView with 3 sections and different rows, I want to add check box JUST to my third sections,
I create custom cell and I linke img and label
like this picture:
![enter image description here][1]
and my code for custom cell is :
.h
: UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *checkBox;
#property (strong, nonatomic) IBOutlet UILabel *absenceCode;
#end
.m
#synthesize checkBox;
#synthesize absenceCode;
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
// Initialization code
checkBox.image = [UIImage imageNamed:#"emptycheck-box.png"];
}
return self;
}
#end
and code for UITableViewController
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *staKey = #"Start";
NSString *endKey = #"End";
NSString *absKey= #"Absence";
[contents setObject:[NSArray arrayWithObjects:#"Time: 08:00 Date: Fri,3 Aug, 2012", nil] forKey:staKey];
[contents setObject:[NSArray arrayWithObjects:#"Time: 17:57 Date: Fri,3 Aug, 2012", nil] forKey:endKey];
[contents setObject:[NSArray arrayWithObjects:#"Red",#"Black",#"Blue", nil] forKey:absKey];
[keys addObject:staKey];
[keys addObject:endKey];
[keys addObject:absKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
}
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
CheckBoxTableViewCell *cell = (CheckBoxTableViewCell*)[tableView
dequeueReusableCellWithIdentifier:#"CheckBoxTableViewCell"];
cell.imageView.image=[UIImage imageNamed:#"emptycheck-box.png"];
cell.checkBox.image = image;
cell.absenceCode.text =#"Redddd";
cell.text =contentForThisRow;
return cell;
}
would you please help me
Thanks in advance!
Something like the following should do what you want.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
if (indexPath.section != 2) {
//Set up default cell here.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
cell.textLabel.text =contentForThisRow;
return cell;
}
else {
CheckBoxTableViewCell *cell = (CheckBoxTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"CheckBoxTableViewCell"];
cell.imageView.image=[UIImage imageNamed:#"emptycheck-box.png"];
cell.checkBox.image = image;
cell.absenceCode.text =#"Redddd";
return cell;
}
}