【问题标题】:View last cell when i add array of cell in tableview当我在表格视图中添加单元格数组时查看最后一个单元格
【发布时间】:2013-08-07 01:10:30
【问题描述】:

我需要在表格视图单元格中显示最后一个单元格记录。每次我都会在表格视图中添加新数组。之后我将重新加载 tableview 单元格。现在它显示来自第一个单元格的记录。请任何人告诉我如何显示最后一个单元格。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"TrackDataTimeCell";
TrackDataTimeCell *cell = (TrackDataTimeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    @autoreleasepool {
        [[NSBundle mainBundle] loadNibNamed:@"TrackDataTimeCell" owner:self options:nil];
        cell = (TrackDataTimeCell *) cellChallengeFriends;
        cellChallengeFriends = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}

if (!cell) {
    cell =(TrackDataTimeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    cell.accessoryView = activityIndicatorView;

}
NSArray *tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", nil];
NSArray *tableData1 = [NSArray arrayWithObjects:@"test1", @"test2", nil];


[cell.deleteButton  addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.deleteButton.tag = indexPath.row;


cell.nameList.text= [tableData objectAtIndex:indexPath.row];
cell.marklist.text= [tableData1 objectAtIndex:indexPath.row];


return cell;

}


- (IBAction)addBillingItems:(id)sender
 {

    rowVal=rowVal+1;
    [nameList addObject: self.codeAdd.text];
    [marklist addObject: selectDate];

    [tbl reloadData];

}

请在http://postimg.org/image/g0dzs0r2p/查看示例图片

我有 5 个单元格。现在它显示前四个。滚动后它显示 5 但我希望最后一个单元格第一次显示。请帮帮我

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您可以使用它自动滚动到最后插入的单元格。

        CGPoint bottomOffset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.bounds.size.height + 150);
        if ( bottomOffset.y > 0 ) {
            [self.tableView setContentOffset:bottomOffset animated:YES];
        }
    

    150 值是在tableView 中的单元格后面添加额外的空间。它是可选的,即如果您想要单元格后的额外空间,您可以使用它

    【讨论】:

    • @Ahamed:我还需要一个帮助
    • @Ahamed:有一个文本框。它现在是只读的。这里我想要的意思是只要我点击那个文本框就会调用一个动作。
    • 你想执行一个动作/选择器,在文本框的点击上调用一个方法? ryt?
    • @Ahamed:是的,在我通过单击按钮操作调用该方法之前。现在我想在单击 UITextfiled 时调用方法
    • 在你的 .h 文件中取UITextFieldDelegate,然后在 .m 文件的viewDidLoadtextField.delegate = self; 中添加这个方法你的 .m 文件-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { [self myMethod]; return YES; }
    【解决方案2】:

    您必须少设置 tableview 框架。如果您有 iPhone tableview 框架,则将框架设置为 cgrectmake(0,0,320,430)。如果这不能解决您的问题,请添加 uitableview:viewforheaderinsection 方法。只需添加 uiview 并设置框架为 cgrectmake(0,0,320,1)。这将为表格设置框架并显示所有单元格。

    【讨论】:

      【解决方案3】:

      您的问题有一个简单的解决方案。只需使用tableView的方法

          [tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForRow: rowVal inSection:0]
                            atScrollPosition:UITableViewScrollPositionBottom
                                    animated:YES];
      

      [tbl reloadData]之后调用就行了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        • 2013-03-06
        • 1970-01-01
        • 2020-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多