where can find a example on adding UIButton in each UITableCell - uibutton

Where can find a example on adding UIButton in each UITableCell?

You need to create a UIbutton and jus addSubView to the cell it will work fine.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UIImageView * accerTypeImage;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *button = [[UIButton allo] init];
button.frame = //give the frame in UITableViewCell.
button.tag = indexPath.row;
[cell addSubView:button];
[button release];
return cell;
}

Related

UITableView -cell buttons not showing up

At one point my cell buttons were showing up when my scroll view was much wider. I made it smaller and my buttons have disappeared. I've played around with the frame but it doesn't seem to work. Any suggestions?
- (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]autorelease];
UIButton*button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10.0, 0.0, 20,20);
[button setTitle:#"Tap" forState:UIControlStateNormal];
button.backgroundColor= [UIColor blackColor];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
[cell.contentView addSubview:button];
}
NSString *cellValue = [selection objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
you have to implement custom button outside if condition as u wrote. you will have to write - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method as below:
- (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];
}
//custom button outside if condition
UIButton*button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10.0, 0.0, 20,20);
[button setTitle:#"Tap" forState:UIControlStateNormal];
button.backgroundColor= [UIColor blackColor];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
[cell.contentView addSubview:button];
return cell;
}
For the moment get rid of this statement:
cell.accessoryView = button;

How to set separator of one section of tableView to clear

I have four sections in my tableView, and I want only the last section to have a clear separator. I know how to do this for the whole tableView, but cannot find any solution for my question. The only solution that I have thought of is to create the cell as an image in Photoshop and set that image as the cell background. Does anyone have an idea of how to do this without having to use Photoshop?
Try this one
In View didLoad
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
And
-(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];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)];
view.backgroundColor = [UIColor greenColor];
view.tag = 101;
[cell addSubview:view];
}
if (indexPath.section == 0)
{
[[cell viewWithTag:101] setHidden:YES];
}
else if(indexPath.section == 1)
{
[[cell viewWithTag:101] setHidden:NO];
}
return cell;
}

Add background image to all UITableViewCell

I used willDisplayCell delegate method to show a custom background image to a UITableViewCell.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
static UIImage* bgImage = nil;
if (bgImage == nil) {
bgImage = [UIImage imageNamed:#"myImage.png"];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
}
When I run the app in the simulator, the background only for the cells that are drawn are changed, is there a way to change all cells' background?
I have worked this code.It's working good .you can try this code.
- (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] autorelease];
}
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
UIImageView *cellBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
cellBackView.backgroundColor=[UIColor clearColor];
cellBackView.image = [UIImage imageNamed:#"list.png"];
cell.backgroundView = cellBackView;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
You have to set the backgroundView property in the cellForRowAtIndexPath method that creates the cell:
- (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];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:...];
return cell;
}

UITextView in a grouped UITableView's cell

Please, explain me: how to programmatically create an UITextView in UITableViewCell (UITableView has a grouped style). The text view size should be equal to size of the cell.
The UITextView has a gap between its text and its border (top left corner), so I need the correct coordinates to properly place the text view on a cell.
UPDATE: I've solved my question. See my self-answer below. Thanks!
It was simple:
textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
Here you go:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Prototype Cell"];
UITextField *textfield;
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Prototype Cell"];
textfield = [[UITextField alloc] init];
[cell.contentView addSubview:textfield];
textfield.tag = TEXT_FIELD_TAG; //Suppose you defined TEXT_FIELD_TAG someplace else
}
else
{
textfield = [self.view viewWithTag: TEXT_FIELD_TAG];
textfield.frame = cell.frame;
}
return cell;
}
Set "contentInset" of UITextView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *sCellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail
reuseIdentifier:sCellIdentifier];
}
UITextView *textView = [UITextView alloc]init];
textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //Change according ur requirement
textView.frame = cell.frame;
[textView setUserInteractionEnabled:FALSE];
[textView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubView:textView];
return cell;
}

Adding a UIPickerView to a UITableView cell

I am trying to add a pickerView to a row in my table view, but after doing this, all I see is a black rectangle inside the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
pickerView = [[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200, 300)] autorelease];
[cell.contentView addSubview:pickerView];
}
}
I really don't have a clue how to solve this.
Can anyone help me, please?
May be this can help: DateCell with TableViewController