How can we select tableList row bydefault - ios

Hi i am very new for ios and in my app i have created
one UItablaList ok fine
here by default my tableList row one textcolor
i have assigned like "RED"
and when i click on another rows that first row color must be "WHITE" and selected row color must be "RED" and if i click dy default selected first row then that color must be "RED please help me some one ,How can we implement logic for this
my code:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"MyCell";
MyCellTableViewCell *cell = (MyCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
UILabel *lineLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, RightMenuTbl.frame.size.width, 1)];
lineLbl.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:lineLbl];
//remove background for table:-
if (indexPath.row == 0) {
lineLbl.textColor = [UIColor redColor];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

Take int as selectedRow in .h file and in didselectrow
selectedRow = indexPath.row;
[yourtableview reloadData];
and in cellforrow
if (selectedRow== indexPath.row) {
lineLbl.textColor = set color you want for selected
}else
lineLbl.textColor = set color you want for not
selected

try this code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath * cellIndexPath = indexPath;
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:cellIndexPath];
cell.nameLbl.textColor = [UIColor redColor];
}
(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.nameLbl.textColor = [UIColor whiteColor];
}

Try the below code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.nameLbl.textColor = [UIColor redColor];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.nameLbl.textColor = [UIColor whiteColor];
}

Related

Grabbing the value of specific textlabel and making json in didSelectRowAtIndexPath

Following is my cellForRowAtIndexPath method in MyOrdersController.m file...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#""];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
self.shipmentReferenceNumberTextLabel = [[UILabel alloc]init];
self.shipmentReferenceNumberTextLabel.text = amountArray[indexPath.row];
self.shipmentReferenceNumberTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:10];
tableView.tableFooterView = [UIView new];
cell.layer.borderWidth = 1.0;
cell.layer.cornerRadius = 10;
cell.layer.borderColor = [UIColor blackColor].CGColor;
[cell.contentView addSubview:self.productAmountLabel];
[cell.contentView addSubview:self.productAmountTextLabel];
return cell;
}
I have got a textlabel shipmentReferenceNumberTextLabel in it. I have to grab the value of shipmentReferenceNumber when a user clicks on a particular cell and I have to make json request with it in a file called APIRequest.m. Im trying grab its value as follows,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
[self.navigationController pushViewController:tabBar animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MyOrdersController *detailVC = [[MyOrdersController alloc]init];
NSLog(#"%#", detailVC.shipmentReferenceNumberTextLabel.text);
}
The problem is that, its printing null while im trying to grab its value. How can I sort out this issue.
You can get value of Lable from an array.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSLog(#"%#",amountArray[indexPath.row]);
}
You will get selected row's label value.

Redraw issue with UITableView using Objective C and Xcode 6

I have a tableView which lists student names and highlights the row green for present or red for absent. If you click the row, the color is changed to reflect and change in status for the student.
However, when a student is marked absent (highlighted red) and then I scroll the row out of view the row turns grey. I would love for the row to remain red (ugh).
Here are the tableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [kidsNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [kidsNames objectAtIndex:(indexPath.row)];
NSString * status=[kidsAbsent objectAtIndex:indexPath.row];
if([status isEqualToString: #"Present"]==1){
cell.contentView.backgroundColor = [UIColor greenColor];
}else{
cell.contentView.backgroundColor = [UIColor redColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CLICKED!");
if([ab isEqualToString: #"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(#"Now Absent %#",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Present"];
NSLog(#"Now Present %#",[kidsNames objectAtIndex:(indexPath.row)]);
}
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CLICKED!");
if([ab isEqualToString: #"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(#"Now Absent %#",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Present"];
NSLog(#"Now Present %#",[kidsNames objectAtIndex:(indexPath.row)]);
}
//send info to the web===============================================================================
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
Try to set cell.backgroundView not cell.contentView.backgroundColor
Or use cell.contentView.backgroundColor but you should note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath: method (from UITableViewCell reference):

Accessory type for Cross Sign in iOS

I have a table view and 2 custom cells inside it. I want to use a (X) symbol i.e. the cross symbol and the tick symbol. I know for tick we have UITableViewCellAccessoryCheckmark but what to do for the (X) symbol. Can we have any custom accessory for this or else how to achieve this?
My code:
#pragma mark - Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
optionsLabelArray = [[NSArray alloc] initWithObjects:#"Yes", #"No", nil];
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
if(indexPath.row == 0)
{
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView ==_optionsTableView1) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
previousSelectedCell1.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType=UITableViewCellAccessoryCheckmark;
previousSelectedCell1 = cell;
NSLog(#"Row : %ld", (long)indexPath.row);
if(indexPath.row == 0)
{
self.weatherSafeToPlay = YES;
}
else
{
// MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
// sharedController.actions = [sharedController.actions stringByAppendingString:#"Check Weather. "];
//[sharedController.actions appendString:#"Check Weather. "];
self.weatherSafeToPlay = NO;
}
NSLog(#"Is Weather safe: %hhd", self.weatherSafeToPlay);
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
not heard of any accessory type for cross but in such cases, i simply use unicode characters.
It's lighter than using an image and is a simple matter of just changing the text.
#"\u2713" for ✓
#"\u2717" for ✗
You can:
Create a custom UITableViewCell having a 32by32 (or any dimension) UILabel (on the right/left/middle/wherever)
in -didSelectRowAtIndexPath, you can change the text of this UILabel
#"\u2713" for checkmark or #"\u2717" for crossmark
A simple example (on the default UITableViewCell's default textLabel, just for the idea):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:#"\u2713"]) {
cell.textLabel.text = #"\u2717";
}
else {
cell.textLabel.text = #"\u2713";
}
}
OR... when not using a custom cell, something like:
-(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];
}
//set your text to cell.textLabel.text
//create your own UILabel
UILabel *lblAcc = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
[lblAcc setTag:100];
[lblAcc setText:#"?"];
[lblAcc setBackgroundColor:[UIColor redColor]];
[lblAcc setTextAlignment:NSTextAlignmentCenter];
//set as custom AccessoryView on cell
[cell setAccessoryView:lblAcc];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *strAccessory = [(UILabel *) [cell viewWithTag:100] text];
//the -isEqualToString is a bit heavier than checking a simple bool or int value
//so since you should be remembering the selected cells anyways, you can change
//the if-logic to compare with a bool rather than the string comparison.
//but for now, we'll do:
if([strAccessory isEqualToString:#"\u2713"]) {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2717"];
}
else {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2713"];
}
}
for more unicode characters... this link:
http://en.wikibooks.org/wiki/Unicode/List_of_useful_symbols
You can assign your own image to accessoryView of table cell. Create an image of (X) symbol and use that image as accessoryView.
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Your X Image"]];

TableView Cell contents not shifting in editing mode iOS 6

I have subclass of UITableViewCell. Here is my setup
I have used Autolayout. Now when user swipe on cell , I can see "Delete" button , but it overlaps my right side imageview. Cell contents are not shifting. Here is my code
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recentItemsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"ProductCell";
ProductCell *cell = (ProductCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:#"ProductCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
tableView.separatorColor = [UIColor clearColor];
}
Product *product = [self.recentItemsArray objectAtIndex:indexPath.row];
cell.lblProductName.text = product.name;
cell.lblNoOfBids.text = [NSString stringWithFormat:#"%#",product.numberOfBids];
cell.lblBuyItNowPrice.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.buyItNowPrice];
cell.lblTimeRemaining.text = product.timeRemaining;
cell.lblLatestBid.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.currentBidPrice];
cell.lblUserName.text = product.sellerName;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110.0f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[CommonUtils showAlertWithTitle:#"Alert" andMessage:#"Do you want to delete this product?"];
}
}
I saw several question on Stackoverflow , but didn't get any solution.
Can any body tell me what I am doing wrong ? Any kind of help is highly appreciated. Thanks
Update::
I tried adding UIImageView programatically. still image views are not shifting
- (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];
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(260, 0, 50, 60)];
imageView.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:imageView];
UIImageView* imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(20.0, 0, 60, 60)];
imageView1.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:imageView1];
}
//cell.textLabel.text = #"ABC";
// Configure the cell...
return cell;
}
It happend with me. Verify if some cell property have the same name from other property in the view where the UITableView is.

uitableview first and last visible cell color change

I'm trying to to implement this function in UITableView for iPhone: always have only the FIRST and LAST VISIBLE cell have a different background color, say red, while color of the other cells remain white. and a smooth change during scrolling.
I have tried:
in .m file:
NSIndexPath *firstRow;
UITableViewCell* firstCell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"tableCell";
tableCell *cell = (tableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
//cell.thumbnailImageView.image = image;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSArray *visible = [tableView indexPathsForVisibleRows];
firstRow = (NSIndexPath *)[visible objectAtIndex:0];
firstCell = [tableView cellForRowAtIndexPath:firstRow];
firstCell.contentView.backgroundColor=[UIColor redColor];
NSLog(#"main visible cell's row: %i", firstRow.row);
[tableView endUpdates];
firstCell.contentView.backgroundColor=[UIColor redColor];
}
But the color wont update when scrolling back up.
You can do it all in cellForRowAtIndexPath if you want.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tableViewCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellID];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellID];
[[cell textLabel] setText:[NSString stringWithFormat:#"some sting %i", [indexPath row]]];
NSArray *visibleCells = [tableView visibleCells];
for (int i = 0; i < [visibleCells count]; i++) {
UITableViewCell *cell = [visibleCells objectAtIndex:i];
if (i == 0 || i == [visibleCells count] - 1)
[cell setBackgroundColor:[UIColor redColor]];
else
[cell setBackgroundColor:[UIColor clearColor]];
}
return cell;
}
In your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Try something like this
//Find the first row
if(indexPath.row == 0){
cell.contentView.backgroundColor = [UIColor redColor];
}
And to check for the last row, you should reuse the code you made for
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Resources