向每个表视图单元格添加数字-iOS
在我的应用程序中,我想通过按数字顺序添加数字来显示表视图中有多少单元格。例如,我希望表视图中的第一个单元格旁边有一个1,第二个单元格旁边有一个2向每个表视图单元格添加数字-iOS,ios,objective-c,swift,uitableview,textfield,Ios,Objective C,Swift,Uitableview,Textfield,在我的应用程序中,我想通过按数字顺序添加数字来显示表视图中有多少单元格。例如,我希望表视图中的第一个单元格旁边有一个1,第二个单元格旁边有一个2 谢谢 您可以在tableView:cellforrowatinexpath:方法中执行类似操作 cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row+1]; 编辑: 然后确保执行以下操作: - (NSInteger)tableView:(UITableView *)
谢谢 您可以在
tableView:cellforrowatinexpath:
方法中执行类似操作
cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row+1];
编辑:
然后确保执行以下操作:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10; <<<<====specify here the number of rows to be displayed.
}
现在您有了两个标签,一个用于存储字符串,另一个用于存储子标签,其中包含前面讨论过的行数
添加屏幕截图:
在
表视图中尝试以下操作:cellforrowatinexpath:
实现:
cell.textLabel.text = [NSString stringWithFormat:@"%@ %i",[tableData objectAtIndex:indexPath.row], ((indexPath.row)+1)];
您可以这样做,在使用UITableViewCellStyleValue1时,将“textLabel”保留在想要放在那里的任何内容上,并使用“detailTextLabel”在右侧显示行数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
[[cell textLabel] setText:@"Your Label"];
[[cell detailTextLabel] setText:[NSString stringWithFormat:@"%d", [indexPath row]+1]]; //Note the +1 because the first row will have an indexpath 0
return cell;
}
它的工作,但摆脱了我以前的标签。我需要一个在我的上一个标签旁边的标签,你能粘贴来自tableView:CellForRowatineXpath的代码吗:Unheilig给出的答案是完美的-(UITableViewCell*)tableView:(UITableView*)tableView CellForRowatineXpath:(NSIndexPath*)indexPath{static NSString*simpleTableIdentifier=@“SimpleTableItem”;UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];if(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableIdentifier];}cell.textLabel.text=[tableData objectAtIndex:indexPath.row];{}将此行cell.textLabel.text=[tableData objectAtIndex:indexPath.row];替换为cell.textLabel.text=[NSString stringWithFormat:@“%i”,indexPath.row+1];检查tableData数组,如果它有NSString objectIs正在工作!谢谢!在tableData数组中它包含什么值??
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
[[cell textLabel] setText:@"Your Label"];
[[cell detailTextLabel] setText:[NSString stringWithFormat:@"%d", [indexPath row]+1]]; //Note the +1 because the first row will have an indexpath 0
return cell;
}