2

UIButton我只需要在UITableView我的第一个和最后一个数组计数中添加一个。我发现我们可以使用tableFooterView, 在我们的表格视图下方添加取消按钮。但是我怎样才能在我的 tableview 上实现这一点,并且只使用第一个和最后一个数组值呢?这是我的代码,

- (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]; 
    }

现在我在我的表格视图的每个单元格中收到按钮。如何仅将按钮提供给我的第一行和最后一行数组值?提前致谢。

4

3 回答 3

2

将您的 if 条件更改为,

if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
{

它看起来像这样,

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIButton *checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        checkButton1.tag = 100;//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
        [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];
        [cell addSubview:checkButton1]; 
    }

   //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);

   UIButton *checkButton1 = (UIButton *)[cell viewWithTag:100];//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
   if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
    {
       checkButton1.hidden = NO;
    } else {
       checkButton1.hidden = YES;
    }

您不必checkButton1在 .h 文件中声明。如上所示,使其成为局部变量。然后您可以设置隐藏属性以在不同的单元格中隐藏/显示。除了执行上述操作之外,您还可以在自定义UITableViewCell类中创建此按钮并将隐藏属性设置为cell.checkButton1.hidden = YES. 您需要为此进行子类化UITableViewCell

于 2012-12-17T08:55:19.477 回答
1

而不是在 CellforRow 中创建 Button 您可以在 ViewDidload 中创建并将其附加到表格的 FooterView 中。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
    [aButton setBackgroundImage:[[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
    [aButton setTitle:@"Click me" forState:UIControlStateNormal];
    [aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
    [aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
    [self.tableView setTableFooterView:aButton];

你可以为 headerview 做同样的事情。否则你可以使用Viewforheader方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{        
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; // x,y,width,height

    UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    reportButton.frame = CGRectMake(80.0, 0, 160.0, 40.0); // x,y,width,height
    [reportButton setTitle:@"rep" forState:UIControlStateNormal];
    [reportButton addTarget:self 
                     action:@selector(buttonPressed:)
           forControlEvents:UIControlEventTouchDown];        

    [headerView addSubview:reportButton];
    return headerView;    
}
于 2012-12-17T08:58:20.723 回答
0

You should add first button to your table view Header And other button should be added to your footer view Secion of tableView

于 2012-12-17T09:20:20.637 回答