I need to add a UIButton in my UITableView only with my first and last array count. I found that we can use tableFooterView, to add unbutton below our tableview. But how can I achieve this over my tableview and only with first and last array values? Here is my 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];
}
//Adding a UIButton in last row
NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
NSLog(@"lastSectionIndex:%d",lastSectionIndex);
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
NSLog(@"lastRowIndex:%d",lastRowIndex);
// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
NSLog(@"last index:%@",pathToLastRow);
if (pathToLastRow.row == lastRowIndex)
{
NSLog(@"row enters");
checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
[checkButton1 addTarget:self
action:@selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[checkButton1 setBackgroundImage:[[UIImage imageNamed:@"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
editTable.tableFooterView = checkButton1;
[cell addSubview:checkButton1];
}
Now I receive the buttons in every cell of my tableview. How can I give the button only to my first and last row array values? Thanks in advance.
Change your if condition as,
It will look like this,
You dont have to declare
checkButton1in .h file. Make it a local variable as shown above. Then you can set hidden property to hide/show in difference cells. Instead of doing the above, you can also create this button in customUITableViewCellclass and set the hidden property ascell.checkButton1.hidden = YES. You need to subclassUITableViewCellfor that.