gpt4 book ai didi

iphone - 将 cell.textLabel.text 值添加到数组并显示它

转载 作者:行者123 更新时间:2023-11-29 04:49:52 25 4
gpt4 key购买 nike

我有一个 UItableView,每个单元格上有两个按钮。您可以在单元格textLabel中添加或减去1。我将单元格当前值+1 与此相加:

  - (IBAction)addLabelText:(id)sender{
num = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];//<--- num is an NSNumber
number = [[NSMutableArray alloc]initWithObjects:num, nil];//<---- number is an NSMutableArray
[myTableView reloadData];

}

我正在尝试减去文本并将其存储在一个数组中:

    - (IBAction)subtractLabelText:(id)sender
{
if ( [[cell.textLabel text] intValue] == 0){


num = [NSString stringWithFormat:@"%d",[num intValue] +0];
[number addObject:num];
[myTableView reloadData];

}
else{
num = [NSString stringWithFormat:@"%d",[num intValue] -1];
[number addObject:num];
[myTableView reloadData];
}
}

我尝试在 cellForRow 中设置 cell.textLabel.text ,如下所示:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";

cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];


}
cell.textLabel.text = [number objectAtIndex:indexPath.row];//<---IM USING THIS LINE TO SET THE NEW TEXTLABEL
cell.textLabel.text = @"1";


return cell;
}

我的问题

因此,加法有效,但减法无效。当我按下手机上的按钮时它根本不起作用。提前致谢!!

最佳答案

冒着指出显而易见的风险......您似乎已将文本属性硬编码为@“1”。

cell.textLabel.text = [number objectAtIndex:indexPath.row];//<---IM USING THIS LINE TO SET THE NEW TEXTLABEL
cell.textLabel.text = @"1";

第一行可能按照您的想法进行...但随后您立即将其改回@“1”。

编辑 - 根据评论中的说明,我认为您想要执行以下操作。我将修改您自己发布的代码。

请注意,我将添加内容放入 viewDidLoad 中作为示例。您可以在 init 或任何其他地方执行此操作,无论何时您都知道要显示多少个单元格。

- (void)viewDidLoad {
self.number = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < [HOW MANY CELLS DO YOU WANT?]; i++) {
[self.number addObject:@"1"];
}

[myTableView reloadData];
}

- (IBAction)addLabelText:(id)sender {
// Note that I'm assuming here that your button is a direct child of the cell.
// If not, you'll need to change this.
UITableViewCell *cell = [sender superview];
NSInteger newNumber = [cell.textLabel.text intValue] + 1;
NSString *newNumberString = [NSString stringWithFormat:@"%d", newNumber];

[self.number replaceObjectAtIndex:cell.tag withObject:newNumberString];

[myTableView reloadData];
}

- (IBAction)subtractLabelText:(id)sender {
// Note that I'm assuming here that your button is a direct child of the cell.
// If not, you'll need to change this.
UITableViewCell *cell = [sender superview];
NSInteger newNumber = [cell.textLabel.text intValue] - 1;
newNumber = (newNumber < 0) ? 0 : newNumber;

NSString *newNumberString = [NSString stringWithFormat:@"%d", newNumber];

[self.number replaceObjectAtIndex:cell.tag withObject:newNumberString];

[myTableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";

cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}

cell.tag = indexPath.row; // Important for identifying the cell easily later
cell.textLabel.text = [self.number objectAtIndex:indexPath.row];

return cell;
}

我不想深入太多,但我建议您看一下 reloading only the cell modified ,而不是每次都调用 [myTableView reloadData]。

关于iphone - 将 cell.textLabel.text 值添加到数组并显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9013291/

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