gpt4 book ai didi

iPhone sdk 如何在 UITableView 中添加具有第一个和最后一个数组计数的 UIButton?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:35 25 4
gpt4 key购买 nike

我只需要在我的 UITableView 中添加一个 UIButton 以及我的第一个和最后一个数组计数。我发现我们可以使用 tableFooterView,在我们的 tableview 下面添加取消按钮。但是我怎样才能在我的 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];
}

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

最佳答案

将您的 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;
}

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

关于iPhone sdk 如何在 UITableView 中添加具有第一个和最后一个数组计数的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13910928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com