Receive Data from UITableViewCell - objective-c

I have the following code set:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:#"CustomCellView" owner:nil options:nil];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
for(id currentObj in nibObjs)
{
if([currentObj isKindOfClass:[CustomCell class]])
{
cell = (CustomCell *)currentObj;
}
}
}
AssessmentDetail * anAssess = [module.assessmentDetails4 objectAtIndex:indexPath.row];
[[cell labelAssessment] setText:anAssess.assessmentName4];
return cell;
}
In my custom cell there is a UISlider. What I would like to do is use a button to retrieve the value of each UISlider from each row so I can get a total value.
I thought about doing this but I'm not certain on where to go from there:
- (IBAction) calculateTotal:(id)sender {
static NSString *CellIdentifier = #"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
Thanks for the help.

Cycle through the subviews of the UITableView:
- (IBAction) calculateTotal:(id)sender {
NSArray *subViews = [myTableView subviews];
float total;
for (int i = 0; i < subViews.count; i++)
{
id obj = [subViews objectAtIndex:i];
if ([obj isKindOfClass:[CustomCell class]])
{
total += [[obj getMySlider] getValue];
}
}
// do something with total
}

Related

I am using 2 tableviews in single screen, one is plane and second one is custom cell. Only one is working

NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (tableView==self.monthTableVW)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
}
if (tableView==self.attendanceTBVW)
{
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:#"customeCell" owner:self options:nil];
cell=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
}
return cell;
}
I need one table view to use as drop down menu and the second one is to display the data i.e attendanceTBVW which is customCell.
First set different cell identifier for different table view cell in the storyboard identity inspector. Then
#import CustomTableViewCell.h
Then drag the dateLbl IBoutlet property in this class. Then cast the custom cell in this type.
NSString *cellIdentifier = #"cellID1";
NSString *customCell = #"cellID2";
UITableViewCell *cell;
if (tableView==self.monthTableVW)
{
if (cell == nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
}
if (tableView==self.attendanceTBVW)
{
//customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
//cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
(CustomTableViewCell *)cell = [tableView dequeueReusableCellWithIdentifier:customCell];
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:#"customeCell" owner:self options:nil];
cell=[cellArr objectAtIndex:0];
cell.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
}
//cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
}
return cell;
}
Try it. Hope it will work.
From your code it looks you are only returning cell for the drop down menu. You are not returning the cell1 for the attendanceTBVW. Please return individual cells for both table views. Hope it helps.
Can you make it like create different cell for different tableview and return its cell individual.
Because both table are different so you have to return different cell for both.
Do this changes in your code.
if (tableView==self.monthTableVW) {
NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
return cell;
}
if (tableView==self.attendanceTBVW) {
NSString *cellIdentifier = #"customeCell";
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil) {
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
cell=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
return cell;
}
Try to make this changes check it out
if (tableView==self.monthTableVW) {
NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
return cell;
}
if (tableView==self.attendanceTBVW) {
NSString *cellIdentifier = #"customeCell";
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil) {
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
cell=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
return cell;
}
UITableViewCell * raj=[[UITableViewCell alloc]init];
if (tableView==self.monthTableVW)
{
NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
raj= cell;
}
if (tableView==self.attendanceTBVW)
{
NSString *cellIdentifier = #"customeCell";
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell1==nil) {
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
cell1=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
raj= cell1;
}
return raj;
This one worked for me ..thanks alot

Tableview Checkmark not display using iOS storyboard with objective C

I am trying to create tableview with tableviewcell and whenever user select the tableviewcell It should apply checkmark. My tableview and tableview cell everything I have created by iOS storyboard. Please help step by step how to solve my issue.
My Source:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cellID";
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[myTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSString *stringForCell;
if (indexPath.section == 0) {
stringForCell= [myData objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1){
stringForCell= [myData objectAtIndex:indexPath.row+ [myData count]];
}
[cell.sec_Label setText:stringForCell];
return cell;
}
You can do this way,first thing declare the variable's in header files
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cellID";
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[myTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tintColor = [UIColor blackColor];
NSString *stringForCell; // Mention in header file
if (indexPath.section == 0) {
stringForCell= [myData objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1){
stringForCell= [myData objectAtIndex:indexPath.row+ [myData count]];
}
[cell.sec_Label setText:stringForCell];
return cell;
}
write this in your tableview delegate function
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell ....//some creation
cell.accessoryType=UITableViewCellAccessoryCheckmark;
//cell.accessView don't set this, if set, use custom view. ignore accessoryType
return cell;
}

Cell text not turning up-UITableView

I have a UITableView within a View Controller. Very strange because the header is showing up and the header text but not the cell text.
- (void)viewDidLoad
{
table.delegate = self;
selection= [[NSMutableArray alloc] init];
[selection addObject:#"text"];
[selection addObject:#"text"];
[selection addObject:#"text"];
...}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue = [selection objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// cell.textColor=[UIColor blackColor];
return cell;
}
Try adding this line
table.dataSource = self;
This way, all the table view data source methods (including cellForRowAtIndexPath:) are called, so long as your class conforms to the UITableViewDataSource protocol.

Populating 2nd table in Objective C

I have two tables (Cell Identifier: Call and Cell Identifier: Cell2). I'm passing two arrays of Objects (one for each table) however when I go to do this in my tableView my 2nd table is bringing up the same data as my first table and not the data from the 2nd array. My arrays are set globally as NSMutableArray in my .h file. This is where I think the problem is within the code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell.textLabel.text = [array objectAtIndex:indexPath.row];
label1.text = [arrayDate1 objectAtIndex:indexPath.row];
label2.text = [arrayDate2 objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currDate, someOtherKey]; //added
return cell;
//This will Load the second table (myTableView2)
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(!cell2)
{
cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell2.textLabel.text = [array2 objectAtIndex:indexPath.row];
return cell2;
}
It is indeed a problem in this function:
The code as written, will simply run until it gets to the first return cell; line and never run the code after that, therefore always returning an instance of a Cell cell.
In order to use two tables like this, you need to be able to tell them apart. Usually, you store both of them in a declared property. For the rest of this answer, I'll assume that you are doing that and that they are called table1 and table2.
Change you code to look like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.table1]) {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell.textLabel.text = [array objectAtIndex:indexPath.row];
label1.text = [arrayDate1 objectAtIndex:indexPath.row];
label2.text = [arrayDate2 objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currDate, someOtherKey]; //added
return cell;
} else if ([tableView isEqual:self.table2]) {
//This will Load the second table (myTableView2)
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(!cell2)
{
cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
// NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:#"Current Date"]; //added
// NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:#"Some other key"]; //added
cell2.textLabel.text = [array2 objectAtIndex:indexPath.row];
return cell2;
}
}
// If you reach this point, we don't recognize the table and return `nil`, then the program will crash. Handle this however you want.
return nil;

error with checkmark code

In my cellForRow code I have the following and am getting an error saying nothing is being returned. What have I done wrong? Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
This is an endless loop: UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
This should be:
static NSString *reuseIdentifier = #"some_identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]
}
Your cellForRowAtIndexPath will look something like this (for the most Basic Table View)
- (UITableViewCell *) tableView :(UITableView *) tableView cellForRowAtIndexPath :(NSIndexPath *) indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex :row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
return cell;
}