I want to get cell value in tableView by using:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"selected cell: %#", selectedCell);
}
It should be a String but this is what i get in the NSLog:
selected cell: <RewardCategoriesTableViewCell: 0x7ae11290; baseClass = UITableViewCell; frame = (0 92.01; 320 44); autoresize = W; layer = <CALayer: 0x7ae11450>>
And this is my Data Source of Array:
NSArray* data= [[NSArray alloc]initWithObjects:category1, category2, category3, category4, category5, category6, category7, nil];
return data;
This is cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.section >= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.section];
NSString *identifier = [NSString stringWithFormat:#"Cell%ld", indexPath.section];
NSLog(#"%#" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(#"cell value: %#", cell.lblCategory.text);
return cell;
}
Thanks for helps.
In your way, you get the UITableviewCell or subclass
object,so what you log is right.
If you want to get cell value,you should check your dataSource,in
MVC pattern that is your Model.
For example: you have a array named dataArray as Model,Then you get
value dataArray[indexPath.row]
I am not sure why you set text of cell based on the indexPath.section,so ,I post example code based on the code you post
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.section];
NSString * cellText = category.CategoryName;
}
BTY,I think the right way is let text of cell based on the row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*) [tableView dequeueReusableCellWithIdentifier:#"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.row>= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.row];
NSString *identifier = [NSString stringWithFormat:#"Cell%ld", indexPath.row];
NSLog(#"%#" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(#"cell value: %#", cell.lblCategory.text);
return cell;
}
Then in:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.row];
NSString * cellText = category.CategoryName;
}
Related
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
Here is my problem I had a table view which consists of 3 three labels in it (test1, test2, test3) which comes from webservice. I want to load all the three labels in a single table view. Here is my code below :
for (NSDictionary *entry in entries) {
projectNames = [entries valueForKey:#"NM_PROJECT"];
taskNames = [entries valueForKey:#"TASk_NAME"];
subtaskNames = [entries valueForKey:#"SUBTASK_NAME"];
}
NSLog(#"project : %#", projectNames);
NSLog(#"taskNames : %#", taskNames);
NSLog(#"subtask : %#", subtaskNames);
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [projectNames count];
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identitifier = #"Cell";
DLPTSTableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:identitifier
forIndexPath:indexPath];
long row = [indexPath row];
cell.textLabel.text = projectNames[row];
cell.textLabel.text = taskNames[row];
cell.textLabel.text = subtaskNames[row];
return cell;
}
I want to load the test 1 in projectnames array, test 2 in tasknames array and test 3 in subtasks arrays..
Please help me out.
If you are creating custom UITableViewCell with name "DLPTSTableViewCell" then first create three labels i.e yourLabel1, yourLabel2, yourLabel3 and use this below code :
-(DLPTSTableViewCell *)getNewCell
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DLPTSTableViewCell" owner:nil options:nil];
DLPTSTableViewCell *cell;
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[DLPTSTableViewCell class]])
{
cell= (DLPTSTableViewCell *)currentObject;
return cell;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
DLPTSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self getNewCell];
}
// for displaying values
cell.yourLabel1.text = your first value;
cell.yourLabel2.text = your second value;
cell.yourLabel3.text = your third value;
return cell;
}
I am having trouble inputting my string "handle" into a group UITableView that I have;
for(NSDictionary * dataDict in servers) {
NSString * handle [dataDict objectForKey:#"handle"];
}
I'm totally confused on how I could utilize this for statement to dynamically create new cells with the handle.
The actual UITableView is on the main view controller and doesn't have any cells. How can I loop through the values and create the cells with labels?
You've to overrite this datasource methods for your UITableView, don't forget to set datasource for the table. Just reload your data once you fill your server array, [table reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return servers.count; //will create cell based on your array count
}
- (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];
}
//show handle stirng from server array
cell.textlabel.text = [[server objectAtIndex:indexPath.row]objectForKey:#"handle"];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
mycustomcell *cell = (mycustomcell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"mycustomcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog( #"NIB = %#",nib);
NSLog( #"Cell = %#",cell);
}
cell.lblName.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Name"];
cell.lblId.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Id"];
cell.lblGender.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Gender"];
cell.lblAddress.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Address"];
cell.lblPhone.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Phone"];
cell.lblEmail.text = [[arrData objectAtIndex:indexPath.row] objectForKey:#"Email"];
NSLog(#"tbl %#",cell.lblName.text);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
here mycustomcell is Custome cell not tableview's cell... I hope this works for you.. Create new custome cell with named mycustomecell with .h , .m and .xib..
You should use the method below available on UITableViewDataSource protocol.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I am trying to display plist data in tableview.
My problem is that after scrolling down i lost my previously fetched data.
Actually i am using custom cell so i think this is happen.
Code for Source and delegate of table is below.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"Cell";
OffersCustomCell *cell = (OffersCustomCell *)[mytableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"OffersList" ofType:#"plist"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"OffersCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.titleLabel.font = [UIFont fontWithName:#"Formata-Regular" size:13.0f];
cell.saveLabel.font = [UIFont fontWithName:#"Formata-Bold" size:14.0f];
cell.nowLabel.font = cell.titleLabel.font;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *dict = [tableData objectAtIndex:indexPath.row];
NSLog(#"%#",dict);
cell.titleLabel.text = [dict objectForKey:#"title"];
cell.nowLabel.text = [dict objectForKey:#"price"];
cell.saveLabel.text = [dict objectForKey:#"rondel"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[mytableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you set the height for each cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
//calculate height for each cell
return height;
}
Use this UITableView delegate method to set height of the cells.
you can use another class that is store your data and reload time you used that class and restore your data correctly and efficiently.
I solved it. :)
only problem is no retaining memory for UItableview.
tableData = [[NSArray arrayWithObjects:[dic objectForKey:#"Response"], nil] retain];