I have a custom cell with a property that is an NSString called videoURl. My implementation code looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
// Configure the cell...
cell.videoThumbnail.image = [UIImage imageNamed:[thumbNailArray objectAtIndex:indexPath.row]];
NSString *stringWithoutPng = [[thumbNailArray objectAtIndex:indexPath.row]
stringByReplacingOccurrencesOfString:#".png" withString:#""];
cell.titleLabel.text = stringWithoutPng;
cell.dateUploadLabel.text = [videoUploadDateArray objectAtIndex:indexPath.row];
cell.durationLabel.text = [videoLengthArray objectAtIndex:indexPath.row];
cell.viewsLabel.text = [videoViewsArray objectAtIndex:indexPath.row];
cell.videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(cell.videoURL);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RemedyYouTubeTableViewCell *cell = (RemedyYouTubeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
NSString *videoURL = cell.videoURL;
NSLog(videoURL);
NSLog(#"^videourl");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
When it logs the urls at the top everything is fine but when it is called at the bottom nothing prints. What am i doing wrong? Or is there a better way to do it? Thanks!
please replace your didSelectRowAtIndexPath method with this given code...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
videoURL = [videoURLArray objectAtIndex:indexPath.row];
NSLog(videoURL);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
}
Related
I want to checkmark the data when the data is being loaded in the uitablecell. I know it works when you put it in the didselect section but can we use it in the cellForRowAtIndexPath. If yes how to use it? I have tried but it doesn't show any checkmarks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"AddGoalsTableViewCell";
AddGoalsTableViewCell *cell = (AddGoalsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AddGoalsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.addGoalsText.font = [UIFont fontWithName:#"AvenirNext-Medium" size:17];
cell.addGoalsText.textColor = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f];
cell.addGoalsText.text = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"name"];
PFObject *currentUser = [[goalsArray objectAtIndex:indexPath.row] valueForKey:#"user"];
User *user = [[User alloc] initWithUserId:[currentUser valueForKey:#"userID"] userName:[currentUser valueForKey:#"name"] anduserpassword:[currentUser valueForKey:#"password"]];
if(user==nil)
{
}
else if([[user userId]isEqualToString:[[NSString stringWithFormat:#"%#", [[[UIDevice currentDevice] identifierForVendor] UUIDString] ] lowercaseString]])
{
[self.tableView beginUpdates];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark; <------Here
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
return cell;
}
Please refer following code
- (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] autorelease];
}
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.lastIndexPath = indexPath;
[tableView reloadData];
}
And lastIndexPath is a property(retain) NSIndexPath* lastIndexPath;
I'm having the strangest issue; does anyone know why the below table view isn't loading my custom cells? And yes, the datasource and delegate are connected to the view controller :) I've done this before, but all of a sudden it's like something missing.
.h
#interface MyAccountViewController : UIViewController {
IBOutlet UITableView *StorageTableView;
NSArray *StoredItems;
NSMutableData *data;
}
.m
- (int)numberOfSectionsInTableView: (UITableView *)tableview
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [StoredItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *node = [StoredItems objectAtIndex:indexPath.row];
[[cell itemName] setText:[node objectForKey:#"title"]];
[[cell itemDescrip] setText:[[[[node objectForKey:#"body"] objectAtIndex:0] objectForKey:#"raw"] objectForKey:#"value"]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 99;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Did you register your custom cell for use?
UINib *nib = [UINib nibWithNibName:#"StorageItemTableViewCell" bundle:nil];
[self.StorageTableView registerNib:nib forCellReuseIdentifier:#"StorageItemTableViewCell"];
One big problem is that you only setup each cell once. You need to change your code to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = #"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.itemName.text = [[StoredItems objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.itemDescrip.text = [[StoredItems objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}
You also need to make sure that [StoredItems count] is returning a non-zero value.
When my UITableview loads I am getting the same cell over and over. I'm assuming I need to add something about indexpath.row but this is my first time using JSON so I'm not sure exactly how to do it. Attached is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
}
NSArray *subArray = [subDict objectForKey:#"items"];
if ([subArray count]) {
for (NSDictionary *dictItem in subArray) {
cell.titleLabel.text = [dictItem objectForKey:#"title"];
NSNumber *totalSeconds = [dictItem objectForKey:#"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:#"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;
NSString *separatorString = #"T";
NSString *firstUploadString = [dictItem objectForKey:#"uploaded"];
NSString *uploadString = [firstUploadString componentsSeparatedByString:separatorString].firstObject;
cell.dateUploadLabel.text = uploadString;
NSString *viewCount = [NSString stringWithFormat:#"%#",[dictItem objectForKey:#"viewCount"]];
cell.viewsLabel.text = viewCount;
NSDictionary *playBackDict = [dictItem objectForKey:#"player"];
NSString *playBackURL = [playBackDict objectForKey:#"mobile"];
NSLog(playBackURL);
NSDictionary *thumbnailDict = [dictItem objectForKey:#"thumbnail"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *thumbnailURL = [thumbnailDict objectForKey:#"sqDefault"];
NSURL *url = [NSURL URLWithString:thumbnailURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
// Here in main thread set image to your cell imageView.
// e.g.
[cell.videoThumbnail setImage:thumbNailImage];
});
});
};
}
return cell;
}
I'm just having trouble iterating through these so any help would be awesome. Thanks!!
You are iterating over the subArray variable and configuring your cell for each item in the array. So all your cells always are going to have the same data that it correspondes with the last item of your array.
You need to use the indexPath parameters that receive in the (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to get the correct object for each cell, similar to this snippet:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell... if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}
NSArray *subArray = [subDict objectForKey:#"items"];
// You should check here the array size before get the item
NSDictionary *dictItem = [subArray objectAtIndex:indexPath.row];
NSNumber *totalSeconds = [dictItem objectForKey:#"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:#"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;
// ToDo: all your configuration here using dictItem
return cell;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is called for the tableView one time for each row depending on the value returned by (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView and (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
So Whenever I launch my app it looks like this and the first 3 cells are all the same. But once I start scrolling up and down it fixes and cell 2 and 3 show the correct information.
This is currently how my code looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyFeedCell";
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"SampleTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SampleTableCell *)currentObject;
break;
}
}
}
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Here's a gif of how it's currently functioning
http://gyazo.com/2011099496257445c008a717beabd8fd
The whole topLevelObjects gambit is no longer necessary, replace your code with this and see if your still getting the problem:
//this above #interface
static NSString *CellIdentifier = #"MyFeedCell";
//Put this in viewDidLoad
[self.table registerClass:[SampleTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.table registerNib:[UINib nibWithNibName:#"SampleTableCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I am new to UITableViews, and I dont know very much about this topic.
I have two buttons in the same View, but not in the Table, for control the switch between the UITableViewCells, when one button is pressed then set a boolean for load the specific Cell.
If you click the button 1 then it shows the table with the CellType1.
If you click the button 2 then it shows the table with the CellType2.
My goal is use the same Table for this. But I dont know where to start.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
if (clickButton1 == true)
{
clickButton1 *cell = (clickButton1*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = #"Trial";
return cell;
}
else if (clickButton2 == true)
{
clickButton2 *cell = (clickButton2*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//cell.name.text = #"Trial";
return cell;
} else
I think that this code is not the best. But I am lost at this point.
Thanks in advance!
using a tutorial example like this...
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tweetsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
if you have two arrays for items such as self.tweetArray1 and self.tweetArray2 you could change this code to
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int i = buttonNumberClicked;
return [self.tweetsArray[i] count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
//5
int i = buttonNumberClicked;
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray[i] objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
and set up ibactions for your buttons to change the variable/int buttonNumberClicked to 1 or 2 depending on which was pressed and [tableView reloadData]; inside ibaction as well, hope this helps.