- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa
当您使用 UITableViewCell 的内置样式(副标题、右侧细节等)时,您可以使用 textLabel 和 轻松访问文本标签>detailTextLabel 是 UITableViewCell
我是 iPhone 开发新手,目前正在开发一个简单的 RSS 阅读器应用程序。我遇到的问题是我需要重新定位 UITableViewCells 内的 textLabel 。我尝试过 setFrame 或
我希望在按下按钮时让文本标签淡入淡出。我注意到 textlabel 没有不透明度参数。还有其他方法可以达到预期的结果吗? 最佳答案 Winform 不支持Control 不透明度。 在 .Net Fr
好吧,我是一个初学者,我创建了一个带有自定义单元格类的 UITableView,该单元格类按其应有的方式加载,并在每个文本标签中显示 2 个文本标签(cellPrice 和 cellTitle)行/单
我有这个代码: cell.textLabel.text = [arrBugs objectAtIndex:indexPath.row]; cell.detailTextLabel.text = @"j
有一个 UITableViewCell“postCell”,它是一个 IBOutlet,并附加到我放置在 .xib 中的单元格。设置文本标签: - (void)viewDidAppear:(BOOL)
我是 IOS 开发的新手,现在我在 cell.textLabel.textColor 中遇到问题 我所做的是实现一个 TableView ,并根据部分设置颜色,如下所示,我有 4 个部分,每个部分有
我有一个一年前的项目成功了。 我已经编码了: cell.textLabel.numberOfLines = 0; // Multiline 在 UITableView 单元格中启用多行文本,之前它在
我在 swift 中使用部分以编程方式创建了一个 TableView。部分和行的数量将始终相同,因为它们只是显示我从 Firebase 提取的信息。 我想更改其中一个单元格的 textLabel 以对
我正在使用 Swiftforms,发现 here , 添加一个表单供用户填写。由于我已经掌握了大部分信息,因此我正在为用户填写表格,然后如果他们选择并单击提交,他们就可以对其进行编辑。当我预填文本时,
我有自定义 UITableViewCell,但无法在 cellForRow: atIndexPath: 中设置缩进。 cell.indentationWidth = 10.0 cell.indenta
我正在尝试将单元格的 textLabel 文本显示在跨越整个单元格的图像之上。我创建了一个自定义单元格子类,只是为了使图像宽度等于整个单元格大小,如下所示: - (void) layoutSubvi
我希望有一个带有一些标签的表格,然后我在标签后添加一个文本字段 UI。我希望标签的大小固定(例如 150),单元格宽度的其余部分由文本字段占据。 因此我在函数中设置了 cell.textLabel 的
我正在努力使我的 UITableViewCell 的文本标签中的文本从单元格的右侧环绕大约 30 个像素。然而,它从右边开始大约 5 或 10 个像素。我已经尝试编辑我在 cellForRowAtIn
有没有办法让 UIButton 在其 textLabel 中有两种不同的字体大小?原生? 我不想在顶部放置 UILabel。 最佳答案 您可以在界面生成器中执行此操作。此 GIF 将向您展示如何增加文
我有一个 UITableView,作为 cellForRowAtIndexPath 方法,我正在更改单元格的一些属性(字体、大小等)。现在,除了更改 textLabel 的颜色外,下面列出的所有分配都
我几乎尝试了所有方法,但似乎无法将 cell.textLabel 属性向下移动一点点。我在下面附上了一张截图,我已经尝试了几乎所有我能找到的东西。 我尝试直接更改 .frame 属性,尝试通过使用“-
我已经使用代码创建了一个 UITableView 和一个 UITableViewCell 的子类,在我选择单元格之前它显示得非常好。 单元格的textLabel有时会右移或不移 选择前问题的图像表示
我有一个可排序的 UITableView,但仅此而已。现在,我将所有单元格设置为 UITableViewCellEditingStyleNone,但这仍然将单元格的 TextLabel 向右移动 40
我是一名优秀的程序员,十分优秀!