Add Button to separate cell - iphone

I need to add button to separate cells
Button in yellow circle
i can add to all cells like [cell addSubview:myButt]; but it works to all cells, i need only for 5, 6, 7, 8 cells;
Do i need to write a custom UITableViewCell?
And that line(black arrows), as i understand it is Section, how can i create it with no title, with line(image) and in middle of the table? Thank's to all.
P.S. Sorry for my English

You can hide your section header like this :
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0f;
// or 1.0 as per your line
}
For buttons on your cells, you have to create a custom cell Class and then use that in cellForRowAtIndexPath method. You can also use tags for this. You can do it like this:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:"Cell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
if(indexPath.section == 1)
{
if(indexPath.row>=5 && indexPath.row <=8)
{
//adding buttons to custom cell
}
}
}

You could use the tag property of UIView to keep track of your button but I'd really recommend to use a custom UITableViewCell. With that being said, below is the code you could use. It is optimized to not add the same button over and over but just when you need it, and hide it in cases you don't need it.
static int kButtonViewTag = 3294802;
UIButton *button = [cell.contentView viewWithTag:kButtonViewTag];
BOOL shouldDisplayButton = indexpath.row == 5 || indexpath.row == 6 || indexpath.row == 7 || indexpath.row == 8;
// If the button should be displayed and is not
//
if (shouldDisplayButton) {
// Add button here
//
if (!button) {
button = // Init your button
button.tag = kButtonViewTag;
[cell.contentView addSubview:button]
}
else if (button.isHidden) {
button.hidden = NO;
}
}
// If the button should not be displayed but is
//
else if (!shouldDisplayButton && button && !button.isHidden) {
button.hidden = NO;
}
And then for your section:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat tableWidth = tableView.frame.size.width;
CGFloat padding = 10; // Add some padding
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(padding, 0, tableWidth - padding * 2, 1)];
[view setBackgroundColor:[UIColor greyColor]; //your background color...
return view;
}

Just add button in cellforrow method :
If (indexpath.row == 3 || indexpath.row == 4 || indexpath.row == 5 || indexpath.row == 3)
{
// add button here
// tag button here
Button.tag = indexpath.row ;
}

Related

How to display a custom UITableViewCellAccessoryCheckmark

I have a table view that needs a custom UITableViewCellAccessoryCheckmark. The checkmark displays when a row is selected and disappears when anther row is selected and then appears on the last most selected view. That works fine.
the problem arises when I use this line:
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"icon-tick.png" ]];
to add a custom UITableViewCellAccessoryCheckmark. After that code the UITableViewCellAccessoryCheckmark remain on all rows and don't disappear when another row is touched.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int index = indexPath.row; id obj = [listOfItems objectAtIndex:index];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSLog(#"%d",indexPath.row);
if (rowNO!=indexPath.row) {
rowNO=indexPath.row;
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"icon-tick.png" ]];
[self.tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone;
lastIndexPth=indexPath;
}
A much cleaner and cooler way would be to overwrite UITableViewCell like this:
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType
{
// Check for the checkmark
if (accessoryType == UITableViewCellAccessoryCheckmark)
{
// Add the image
self.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YourImage.png"]] autorelease];
}
// We don't have to modify the accessory
else
{
[super setAccessoryType:accessoryType];
}
}
If you have done that, you can continue using UITableViewCellAccessoryCheckmark because your class will automatically replace it with an image.
You should only set the style in your cellForRowAtIndexPath method. Like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [init subclassed cell here, dont forget to use the table view cache...]
cell.accessoryType = (rowNO != indexPath.row ? nil : UITableViewCellAccessoryCheckmark);
return cell;
}
And then, you just have to update rowNO in didSelectRowAtIndexPath to update your data and redraw the cell, like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (rowNO != indexPath.row)
{
rowNO = indexPath.row;
}
[self.tableView reloadData];
}
Also, instead of reloading the whole table with [self.tableView reloadData], you could only reload the two cells that change their style (e.g. checkmark) using reloadRowsAtIndexPaths.
Hm don't know why but i can't add comments so im writing this as answer. The problem with Blauesocke answer is that the AccessoryType wil be not set to UITableViewCellAccessoryCheckmark so you can't check the cell AccessoryType. Is there some way to do it right so the cell AccessoryType will be corect type just another image.
I'm using the method like this :
- (void)setAccessoryType:(UITableViewCellAccessoryType)newAccessoryType
{
[super setAccessoryType:newAccessoryType];
// Check for the checkmark
switch(newAccessoryType)
{
case UITableViewCellAccessoryCheckmark:
self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yorCheckmark.png"]];
break;
case UITableViewCellAccessoryNone:
self.accessoryView = nil;
break;
default:
break;
}
}

How to give image on selected cell in iphone

i want to set image on cell .When i select that cell then only i want to pass image on it i have 3 section in tableview,
first section i have 1row.Second section i have 3row and third section i have 1 row so how to call selected image on cell .If i cliked any cell
i try thi code but it working but i am showing lable also with red color if this view appear on cell background then it hide the label bacground red color only i can see text which is coming on that label but label background color i can't see why this happen .
i put this code in cellfor row at indexpath method
UIView *viewSelected = [[[UIView alloc] init] autorelease];
viewSelected.backgroundColor = [UIColor whiteColor];
cell.selectedBackgroundView = viewSelected;
you can accomplish this in didSelectRowAtIndexPath method.....
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
switch ([indexPath.section]) {
case 0:
{
switch ([indexPath.row]) {
case 0:
{
[cell.imageView setImage:[UIImage imageNamed:#"imageName.png"]];
break;
}
break;
}
}
Separate the section using the check
if (indexPath.section == 0){
//perform selection of section 0
}
else if (indexPath.section == 1){
//perform selection of section 1
}
else if (indexPath.section == 2){
//perform selection of section 2
//Also do the selection of particular row
if (indexPath.row == 0){
//perform selection of row 0 section 2
}
}

Add subview to UITableViewCell: Added several times when scrolling

I have some different cells in my tableView, each with different subviews. Each time a cell dissapear and reapear, the subview is addad on top of the old view, and also is added to other cells. What is the correct way to add subviews to cells without using custom cells?
Thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StatisticsCell";
StatisticsCell *cell = (StatisticsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:#"StatisticsCell" owner:nil options:nil];
for (id currentObject in topLevelObject)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (StatisticsCell *)currentObject;
break;
}
}
}
if (indexPath.section == 0 && indexPath.row == 0)
{
//Add a subview here
[cell addsubview ....
}
else if (indexPath.section == 0 && indexPath.row == 1)
{
//Add a subview here
[cell addsubview ....
}
etc....
Whenever you scroll cell for row method is called, so when your cell be visible, it will add subview to the cell. Place a check that is view added already, make an ivar that is bool, and set it true when you add view, and false when you remove. Like this:
.
.
.
if (indexPath.section == 0 && indexPath.row == 0 && isFirstViewAlreadyAdded== NO)
{
//Add a subview here
[cell addsubview ....
isFirstViewAlreadyAdded = YES;
}
else if (indexPath.section == 0 && indexPath.row == 1 && isSecondViewAlreadyAdded == NO)
{
//Add a subview here
[cell addsubview ....
isSecondViewAlreadyAdded = YES;
}
.
.
.
You can check whether that subView is already added to cell or not.
UIView *subView = [tableCell viewWithTag:tagOfYourSubView];
if (subView) {
//subView exists
}
else {
//subView does not exist
}
If it is not added then you can add.
Don't add the subview every time..
You should add the subview in the if(cell==nil) block.
And after that you can set the hidden property as true or false according to indexpath.row.
like:
if (indexpath.row == 0)
img1.hidden = FALSE;
else
img1.hidden = TRUE;

Getting a button press from a custom UITableViewCell's button

I have a project with a custom UITableViewCell (cell.h/.m/.xib) that has 2 labels (labelMain/labelSub), and 2 buttons on it (buttonName/buttonInfo) linked to 2 actions (showName/showInfo).
What I want to do is be able to access the 2 actions in my projects main viewcontroller so that when showName is pressed, a textfield.text in the viewcontroller (not in the cell) is set to that specific cell's labelMain.text.
Hope that makes sense. My problem is that if I write the action (showName) in the cell.m, I cannot access the textfield from my main viewcontroller. On the flip side, if I write the action in my viewcontroller, how do I know which button inside which cell was tapped?
Hope this makes sense...
Use tag can identify which button in which cell is being tapped.
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//init identifier
if (cell ==nil)
{
//load nib file
}
buttonName.tag = indexPath.row;
[buttonName addTarget:self action:#selector(showName:) forControlEvents:UIControlEventTouchUpInside];
buttonInfo.tag = indexPath.row;
[buttonInfo addTarget:self action:#selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void) showName:(UIButton*)button{
int row = button.tag; //you know that which row button is tapped
}
-(void)showInfo:(UIButton*)button{
int row = button.tag;//you know that which row button is tapped
}
---------------- EDIT -------------
If you need to know which button is pressed based on row & section, you may try this below. (in cellForRowAtIndexPath: method)
int tag = (indexPath.row+1)+(indexPath.section*100);
buttonName.tag = tag;
When button at
row = 5, section = 0 then tag = 6.
row = 4, section = 3 then tag = 305.
row = 0, section = 11 then tag =1101.
limitation, row cannot be more than 99. and DON'T use positive tag for other view. if you need use tag, try negative. (-1, -9, -100).
so from here, you can calculate back row & section of indexPath. using this :
-(NSIndexPath*)getIndexPathFromTag:(NSInteger)tag{
/* To get indexPath from textfeidl tag,
TextField tag set = (indexPath.row +1) + (indexPath.section*100) */
int row =0;
int section =0;
for (int i =100; i<tag; i=i+100) {
section++;
}
row = tag - (section*100);
row-=1;
return [NSIndexPath indexPathForRow:row inSection:section];
}
how to use :
-(void)showInfo:(UIButton*)button{
NSIndexPath *indexPath = [self getIndexPathFromTag:button.tag];
int row = indexPath.row;
int section = indexPath.section;
}
Try this in viewController.m file in cellForRowAtIndexPath method
[[customCell showNameBtn] setTag:indexPath.row];
[[customCell showNameBtn] addTarget:self action:#selector(showName:) forControlEvents:UIControlEventTouchDown];
-(void) showName:(id)sender
{
// check here the sender tag and then perform you action what you want.
}
I suggest to use delegate(protocol) method for this. You will be having full control on the button, cell and the view controller. And its very simple too
The simplest solution in my opinion:
In your CustomCell.h :
#property (nonatomic, copy) void (^buttonTapAction)(id sender);
In your CustomCell.m create the IBAction and connect it to your UIButton :
-(IBAction)cellButtonIsPressed:(id)sender{
if (self.buttonTapAction) {
self.buttonTapAction(sender);
}
}
Then in the ViewController.m where your UITableView is managed :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = #"customCell";
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.buttonTapAction = ^(id sender){
//Do what you want to do when the button inside THIS cell is clicked
};
}
Try to create the button dynamically, with its methods in same controller. Do this in cellForRowAtIndexPath: method:
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"WorkRequestedCC" owner:self options:nil];
{
for (id oneObject in nib) if ([oneObject isKindOfClass:[WorkRequestedCC class]])
cell = (WorkRequestedCC *)oneObject;
}
UILabel *costLbl=[[UILabel alloc]initWithFrame:CGRectMake(792, 13, 10, 15)];
costLbl.text=#"$";
[costLbl setBackgroundColor:[UIColor clearColor]];
UITextField *txtCost=[[UITextField alloc]initWithFrame:CGRectMake(805, 10, 94, 27)];
txtCost.backgroundColor=[UIColor whiteColor];
[txtCost setKeyboardType:UIKeyboardTypeNumberPad];
txtCost.text=obj.expCost;
[txtCost addTarget:self action:#selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
[cell.contentView addSubview:costLbl];
[cell.contentView addSubview:txtCost];
cell.lblDes.text=obj.expDes;
}
- (void)textChanged
{
//your code here
}

How to use different cell in one table view?

How to use 3 different cells on one uitableview? I have 3 buttons now When i press any button then cell of table view should be changed. Please help
In the button's action method, save which button was tapped & reload the row in which you want to change the cell.
-(void)button1Tapped:(id)sender
{
self.buttonIndex = 1;
NSArray* arr = [[NSArray alloc] initWithObjects:self.indexPathOfConcernedCell, nil];
[self.tableView reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationNone];
[arr release];
}
Then, in cellForRowAtIndexPath: return the cell one the basis of which button was pressed-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath == self.indexPathOfConcernedCell)
{
if(self.buttonIndex == 1)
{
Cell1* cell = (Cell1*) [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier1"];
if(cell == nil)
{
cell = [[[Cell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier1"] autorelease];
}
}
else if(self.buttonIndex == 2)
{
//do similar stuff
}
//do similar stuff for buttonIndex 3 here
}
}
else
{
//your regular stuff
}
return cell;
}