我想通过使用:
在tableView中获取单元格值-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"selected cell: %@", selectedCell);
}
它应该是一个字符串,但这是我在NSLog中得到的:
selected cell: <RewardCategoriesTableViewCell: 0x7ae11290; baseClass = UITableViewCell; frame = (0 92.01; 320 44); autoresize = W; layer = <CALayer: 0x7ae11450>>
这是我的数据源:
NSArray* data= [[NSArray alloc]initWithObjects:category1, category2, category3, category4, category5, category6, category7, nil];
return data;
这是cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.section >= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.section];
NSString *identifier = [NSString stringWithFormat:@"Cell%ld", indexPath.section];
NSLog(@"%@" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(@"cell value: %@", cell.lblCategory.text);
return cell;
}
感谢您的帮助。
答案 0 :(得分:4)
UITableviewCell
或subclass
对象,所以你记录的是正确的。dataArray[indexPath.row]
我不确定为什么要根据indexPath.section
设置单元格文本,因此,我会根据您发布的代码发布示例代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.section];
NSString * cellText = category.CategoryName;
}
BTY,我认为正确的方法是让基于行的单元格文本
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"RewardCategoriesTableViewCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RewardCategoriesTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.row>= [categories count]){
return nil;
}
Categories *category = [categories objectAtIndex:indexPath.row];
NSString *identifier = [NSString stringWithFormat:@"Cell%ld", indexPath.row];
NSLog(@"%@" , identifier);
cell.lblCategory.text = category.CategoryName;
NSLog(@"cell value: %@", cell.lblCategory.text);
return cell;
}
然后在:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Categories *category = [categories objectAtIndex:indexPath.row];
NSString * cellText = category.CategoryName;
}