UITableView 移动行
的课程
@interface Lesson : NSObject <NSCoding>{
NSString *time1;
NSString *time2;
NSString *predmet;
NSString *namPrepod;
NSString *zamet;
}
我在 tableView 中有一些 NSMutableArray,其中包含我在 tableview 中移动行 ....................................... ...................................................... ......................................
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if(segmentedControl.selectedSegmentIndex==0)
{
if(fromIndexPath.section==0)
{
NSMutableArray *r = [[Singleton sharedInstance].chetNedel.monday objectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row];
}
if(fromIndexPath.section==1)
{
NSMutableArray *r = [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row];
}
但是当我在程序中移动行时我在这行有错误
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
..........
if(segmentedControl.selectedSegmentIndex==0)
{
time1 = [[[UILabel alloc] init] autorelease];
time1.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
time1.tag = tagtime1;
time1.backgroundColor = [UIColor clearColor];
if(indexPath.section ==0)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.monday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==1)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==2)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.wednesday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==3)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.thirsday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==4)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.friday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==5)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.saturday objectAtIndex:indexPath.row] time1];
time1.frame = CGRectMake(8, 12, 300-124, 11);
...........
[cell.contentView addSubview:time1];
我做错了什么?
I have some NSMutableArray in tableView which contain class Lesson
@interface Lesson : NSObject <NSCoding>{
NSString *time1;
NSString *time2;
NSString *predmet;
NSString *namPrepod;
NSString *zamet;
}
I move row in tableview................................................................................................................................
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if(segmentedControl.selectedSegmentIndex==0)
{
if(fromIndexPath.section==0)
{
NSMutableArray *r = [[Singleton sharedInstance].chetNedel.monday objectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row];
}
if(fromIndexPath.section==1)
{
NSMutableArray *r = [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row];
}
but when I move row in program I have got error on this lines
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
..........
if(segmentedControl.selectedSegmentIndex==0)
{
time1 = [[[UILabel alloc] init] autorelease];
time1.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
time1.tag = tagtime1;
time1.backgroundColor = [UIColor clearColor];
if(indexPath.section ==0)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.monday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==1)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==2)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.wednesday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==3)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.thirsday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==4)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.friday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==5)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.saturday objectAtIndex:indexPath.row] time1];
time1.frame = CGRectMake(8, 12, 300-124, 11);
...........
[cell.contentView addSubview:time1];
what i do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你到底得到了什么错误?但是,查看您的代码,我认为当您先执行removeObjectAtIndex,然后执行insertObject:atIndex: 时,就会出现问题。删除对象后,将重新计算数组的索引,现在数组中少了 1 个对象。因此,如果您随后执行 insertObject:atIndex 而不减少要放置它的索引,那么您最终不会将其放置在您想要的位置。我建议您将这两行合并为一行并使用 ExchangeObjectAtIndex:withObjectAtIndex: 代替。这是更新的代码:
What error are you getting exactly? Looking at your code however I think the problem happens when you do the removeObjectAtIndex followed by insertObject:atIndex:. After you remove the object, the indexes of the array are recalculated and now you have 1 less object in your array. So if you then do the insertObject:atIndex without decreasing the index where you want to put it, you're not going to end up with it where you want. I suggest you combine both lines into one and use exchangeObjectAtIndex:withObjectAtIndex: instead. Here's updated code:
我没有找到你的错误,但我认为你必须使用自定义 UITableViewCell 而不是在每一行添加 UILabele ...因为单元格被回收并且 time1 可能已经存在。
i don't find your error but i think you must use a custom UITableViewCell instead of add an UILabele at every row...because cell are recycled and time1 may already exist.