starwar2012 2012-12-29 06:51 采纳率: 0%
浏览 2849
已采纳

在UITableView中添加数组首尾处的UIbutoton

问题:我要在UITableView中添加UIButton,并且只加在第一个和最后一个数组上,不知道应该怎么实现?我知道可以用tableFooterView添加按钮,但是不知道怎么样实现添加到指定数组位置中。请多多指教,谢谢

我的代码:

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

现在我可以在tableview的每个单元里面接受到Button。

  • 写回答

1条回答 默认 最新

  • lanhailvcao 2012-12-29 08:16
    关注

    先修改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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?